GlobalExceptionHandler
Let’s try to handle the runtime exceptions that may happen inside our REST APIs at any time, at any location.
If there is some runtime exception that happens inside your application? You should be able to send a proper response to the client application that so-and-so issue occurred so that they are aware that API invocation is failed.
Exception is a class that can represent all kinds of exceptions, including checked and unchecked exceptions.
With this, the method that I’m going to define right now is going to handle all kinds of exceptions.
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponseDto> handleGlobalException(
Exception exception,
WebRequest webRequest)
{
ErrorResponseDto errorResponseDTO = new ErrorResponseDto(
webRequest.getDescription(false),
HttpStatus.INTERNAL_SERVER_ERROR,
exception.getMessage(),
LocalDateTime.now()
);
return new ResponseEntity<>(errorResponseDTO,
HttpStatus.INTERNAL_SERVER_ERROR);
}
Spring Boot framework is going to look for handler methods which are with the exact exception details.
If there is no such handler exception, then the logic inside this exception handler is going to be invoked.
With the above code in place, now our application can handle the runtime exceptions and it can execute the logic that we have written here.
If needed in real time applications, you can enhance this method to trigger an email to operation or to make an entry into one of the tables where you are maintaining all the exception details for reporting purposes. So the scenario can be anything based upon your business requirements.
How to test a runtime exception ?
Let’s try to introduce a runtime exception intentionally.
Remove the @AllArgsConstructor annotation on your controller class. If you do so, the constructor autowring won’t take place for your service field. All the methods that you invoke will generate NullPointerException.
How to implement global exception logic inside an application ?
So please remember that we need to use this annotation like @ControllerAdvice and we need to write a method and on top of this method we need to make sure we are mentioning this annotation @ExceptioHandler.
The combination of @ControllerAdvice and @ExceptionHandler is going to help you to implement the global error logic inside all of your Controller methods.
GlobalExceptionHandler Full Code
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponseDto> handleGlobalException(
Exception exception,
WebRequest webRequest)
{
ErrorResponseDto errorResponseDTO = new ErrorResponseDto(
webRequest.getDescription(false),
HttpStatus.INTERNAL_SERVER_ERROR,
exception.getMessage(),
LocalDateTime.now()
);
return new ResponseEntity<>(
errorResponseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ErrorResponseDto> handleResourceNotFoundException(
ResourceNotFoundException exception, WebRequest webRequest)
{
ErrorResponseDto errorResponseDTO = new ErrorResponseDto(
webRequest.getDescription(false),
HttpStatus.NOT_FOUND,
exception.getMessage(),
LocalDateTime.now()
);
return new ResponseEntity<>(errorResponseDTO, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(CustomerAlreadyExistsException.class)
public ResponseEntity<ErrorResponseDto> handleCustomerAlreadyExistsException(CustomerAlreadyExistsException exception,
WebRequest webRequest)
{
ErrorResponseDto errorResponseDTO = new ErrorResponseDto(
webRequest.getDescription(false), // to get API information
HttpStatus.BAD_REQUEST,
exception.getMessage(),
LocalDateTime.now()
);
return new ResponseEntity<>(errorResponseDTO, HttpStatus.BAD_REQUEST);
}
}