Hello,
If an exception is thrown, it should be passed to the HandlerExecutionChain
which in turn passed it to any existing HandlerInterceptor
that can used it for any business logic inside afterCompletion()
method.
In fact, when an exception is thrown and handled, the afterCompletion()
method of the interceptor (aka. HandlerInterceptorAdapter
) has no clue on that exception since the DispatcherServlet
pass null
even if there is an exception.
A business case that can use this exception is statistics or logging (mail, jms, ...).
Thank you.
Comment From: pivotal-issuemaster
@omradouane Please sign the Contributor License Agreement!
Click here to manually synchronize the status of this Pull Request.
See the FAQ for frequently asked questions.
Comment From: pivotal-issuemaster
@omradouane Thank you for signing the Contributor License Agreement!
Comment From: omradouane
Hello,
I found a workaround. The method processHandlerException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
may save the exception as an attribute in the request.
So to get the exception, all you need to do is request.getAttribute(DispatcherServlet.EXCEPTION_ATTRIBUTE);
Thank you !
Comment From: rstoyanchev
@omradouane indeed, HandlerInterceptor#afterCompletion
says it does not pass handled exceptions:
* @param ex any exception thrown on handler execution, if any; this does not
* include exceptions that have been handled through an exception resolver
The option remains to check DispatcherServlet.EXCEPTION_ATTRIBUTE
:
* Name of request attribute that exposes an Exception resolved with a
* {@link HandlerExceptionResolver} but where no view was rendered
* (e.g. setting the status code).
So this is by design and we can't change those contracts.