I have a spring boot application and I am working to configure a global exception handler. Based on the latest spring docs I am using @RestControllerAdvice to try and handle the exceptions. Here is an example of my globalExceptionHandler:
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ExceptionResponse> handleException(Exception exp) {
// log the issue
System.out.println(exp);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(
ExceptionResponse.builder()
.errorCode(BusinessErrorCodes.INTERNAL_SERVICE_ERROR.getCode())
.description(BusinessErrorCodes.INTERNAL_SERVICE_ERROR.getDescription())
.error(BusinessErrorCodes.INTERNAL_SERVICE_ERROR.getDescription())
.build());
}
}
My BusinessErrorCode is a class I defined with some business errors + descriptions. However, when I try to trigger this byy forcing an exception I can see it is not working. I am using the sprin-boot security dependency.
Anything I am missing?
Thanks!