Context
We started using CompletableFutureit in our application , but when we introduce it into existing code, we sometimes have to call get()(or join()).
These methods transfer any exception that occurred during the process to ExecutionException(respectively a CompletionException).
Problem
By default, Spring Web MVC will handle these exceptions, returning 500 HTTP status. Instead, we would like to expand their cause and delegate their processing to the appropriate special exception handler.
Unlike what is said in the handling of wrapped exceptions in Spring mvc , re-creating the cause does not work¹ and Spring just writes a debug message with it .
On the other hand, How to remove an exception as an @ ResponseStatus-annotated exception in Spring @ExceptionHandler? is too specific because it assumes that we know in advance the reason for the exception and how to eliminate it.
The solution we found is to define a @ControllerAdvice, which delegates the processing of the cause back HandlerExceptionResolver, like this
@Autowired
HandlerExceptionResolver handlerExceptionResolver;
@ExceptionHandler(ExecutionException.class)
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, ExecutionException e) {
return handlerExceptionResolver.resolveException(request, response, null, (Exception) e.getCause());
}
However, this is a bit like hacking. I am afraid that it might break in future versions of Spring, or if for some reason in the context of more than one HandlerExceptionResolver.
What is the right way to do this?
¹ : , , , .