I would like to know best practice on the following case(s) regarding Rest API Responses.
First code piece
@GetMapping("/person/{id}")
public ResponseEntity<Person> postPersonDetails(@PathVariable("id) Long id) {
Person person = personService.getPersonById(id);
if (null == person) {
throw new ResourceNotFoundException().withMessage("PERSON_NOT_FOUND");
}
return new ResponseEntity<Person>(person, HttpStatus.OK);
}
Second code piece
@GetMapping("/person/{id}")
public ResponseEntity<Person> postPersonDetails(@PathVariable("id) Long id) {
Person person = personService.getPersonById(id);
if (null == person) {
return new ResponseEntity<Person>(null, HttpStatus.NotFound);
}
return new ResponseEntity<Person>(person, HttpStatus.OK);
}
The question is
Which one is better for responding error message to API consumer (user)?
Throwing exception leaves error message in the log. If throwing exception is better, how should I avoid from leaving error messages which come from exceptions thrown intentionally?
Thanks