I'm working with spring boot and I'm dealing with a good way to handle exceptions:
I have this case where interact with a repository to get a product. But what about if there is a connection issue with the DB, I'm not going to catch that exception
Product product = productRepository.findById(productId)
.orElseThrow(() -> new NotFoundException("Could not find a product"));
try {
// Product logic
} catch(Exception e) {
throw new ProductException(String.format("Error getting product productId=%s. Exception message: %s",
productId, e.getMessage()), e);
}
I have a controller advice to catch the exception and return a nice response:
@ControllerAdvice
public class ExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({NotFoundException.class})
public ResponseEntity<Error> handleNotFoundException(HttpServletRequest request, Exception exception) {
.....
}
I think I could do something like this:
try {
Product product = productRepository.findById(productId)
.orElseThrow(() -> new NotFoundException("Could not find a product"));
} catch(NotFoundException e) {
throw new NotFoundException(e.getMessage())
} catch(Exception e) {
throw new ProductException(String.format("Error getting product productId=%s. Exception message: %s",
productId, e.getMessage()), e);
That worked but it looks weird since I'm throwing NotFoundException twice. Any idea?
Generally speaking, there is nothing weird catching an Exception just to throw it again right after. You may for example catch it to log it somewhere and then throw it to the next handler for real handling (like in your case).
You can simply write:
However, for the part which comes after, catching generic
Exception
is usually a bad practice. There are too many sources this exception may come from, and you can't just assume that it is because of the DB not responding.So the best would be that your function catches what it knows how to handle, and throws what it doesn't know how to handle. For example:
If there is an exception, something went wrong. You can't just wrap it and pretend it was a
ProductException
, so:NotFoundException
)ProductException
you will probably hide to the system a bigger issue.