So far now, I've tried;
1) to create a bean which extends org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver and implement doResolveException(..) method (It was waste of time because it already returns ModelAndView). The code is below:
@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
response.setStatus(534);
log.trace("exception from HandlerExceptionResolver");
log.error(ex.getMessage(),ex);
Client bugsnag = new Client(BUGSNAG_API_KEY);
//bugsnag code omitted
ModelAndView modelAndView=new ModelAndView("error");
return modelAndView;
}
2) to create a bean which implements javax.servlet.Fiter and implement doFilter(..) method (It was waste of time, too. Because it already returns ModelAndView)
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(request, response);
} catch (Exception ex) {
errorHandler.handleError(ex);
log.trace("exception from HandlerExceptionResolver");
log.error(ex.getMessage(),ex);
Client bugsnag = new Client(BUGSNAG_API_KEY);
//bugsnag code omitted
RequestDispatcher error = request.getRequestDispatcher("error");
error.forward(request,response);
}
}
3) and the @ControllerAdvice annotation (this works all other exception except view exceptions like parsing)
All 3 solutions didn't work for me to catch view exception and navigate to error page. I think there must be a solution in filter level. But i couldn't manage it. (That's why methods return ModelAndView object does not work)
Any suggestion?
@ControllerAdvice
annotation cannot work, because you can only use it for exception inside a controller, and here you want to process exception in a view. I'm unsure forAbstractHandlerExceptionResolver
, but doc states Spring HandlerExceptionResolver implementations deal with unexpected exceptions that occur during controller execution. But Filter method should work. Could you elaborate on you problems with it ?DelegatingFilterProxy
(cf : stackoverflow.com/a/28763222/3545273)targetFilterLifecycle=true
is seldom useful for aDelegatingFilterProxy
. But is the bean name reallyerrorHandlerFilter
?