I have a Spring Boot application and I just added Sentry to report and track errors. The problem I'm having is that there are certain exceptions that I handle in my application that are not erroneous conditions. For example, I an action with a @Valid
annotated parameter:
@RestController
@RequestMapping("/v1/current-account")
public class CurrentAccountController extends Controller {
@PutMapping
public ResponseEntity<?> updateAccount(@RequestBody @Valid Account account, Authentication auth) {
...
When the account is invalid, it raises MethodArgumentNotValidException
. Because of other cases similar to that, I have an exception handler that looks like this:
@ControllerAdvice
public class RestExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleValidationError(MethodArgumentNotValidException ex, HttpServletRequest request) {
// ...
}
@ExceptionHandler(NotFoundException.class)
public ResponseEntity<?> handleNotFoundException(NotFoundException ex, HttpServletRequest request) {
// ...
}
@ExceptionHandler(BadRequestException.class)
public ResponseEntity<?> handleValidationError(BadRequestException ex, HttpServletRequest request) {
// ...
}
}
which essentially turns some exceptions into responses that are not errors.
When I added Sentry, following the documentation:
@Configuration
public class FactoryBeanAppConfig {
@Bean
public HandlerExceptionResolver sentryExceptionResolver() {
return new SentryExceptionResolver();
}
@Bean
public ServletContextInitializer sentryServletContextInitializer() {
return new SentryServletContextInitializer();
}
}
my custom exception handlers are no longer used. Instead, the exception gets reported to Sentry.
How do I make my exception handlers happen before Sentry?