Reputation: 269
Spring Boot 2.0.5
Would this be considered a "bad practice" why or why not
@DeleteMapping(value = "/{id}")
public Long testDeleteContactWithException(@PathVariable Long id) throws Exception {
return contactService.testDeleteContactWithException(id);
}
within the service there is a user defined exception which bubbles up.
I did see I could do a try/catch then using a ResponseEntity set a message, status code etc. but then it is more code to do that so unless there is some reason why do it.
Upvotes: 0
Views: 344
Reputation: 94
Your clients may hope there are a list of status that can help their works.
And there are a way to catch all exceptions.
@ExceptionHandler
@ResponseBody
public ResponseEntity exp(HttpServletRequest request, Exception ex) {
logger.error("BaseController.exp() => Error:", ex);
ResponseEntity responseEntity = new ResponseEntity();
if (ex instanceof YourException) {
responseEntity.setCode(((YourException) ex).getCode());
responseEntity.setMsg(((YourException) ex).getMsg());
} else {
responseEntity.setCode(ResponseState.ERROR.getValue());
responseEntity.setMsg(ex.getMessage());
}
return responseEntity;
}
Upvotes: 1