3

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?

cengha
  • 173
  • 1
  • 2
  • 9
  • `@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 for `AbstractHandlerExceptionResolver`, 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 ? – Serge Ballesta Jul 07 '15 at 10:24
  • @SergeBallesta actually, i think the filter solution should work, too. I defined my filter with mappings in web.xml, created a bean, but nothing happens (doesn't navigate my error pages or logs the exception manually). also there is a thymeleaf log like --> ErrorHandler.fatalError:57 qtp1732250066-18 [THYMELEAF][qtp1732250066-18] Fatal error during parsing. – cengha Jul 07 '15 at 12:04
  • Are you sure your filter is correctly initialized and called ? You say it is a bean, but it should either be a plain filter (outside Spring), or you should use a `DelegatingFilterProxy` (cf : http://stackoverflow.com/a/28763222/3545273) – Serge Ballesta Jul 07 '15 at 12:22
  • @SergeBallesta here is my web.xml filter errorHandlerFilter org.springframework.web.filter.DelegatingFilterProxy targetFilterLifecycle true errorHandlerFilter /* – cengha Jul 07 '15 at 13:03
  • `targetFilterLifecycle=true` is seldom useful for a `DelegatingFilterProxy`. But is the bean **name** really `errorHandlerFilter` ? – Serge Ballesta Jul 07 '15 at 13:14
  • @SergeBallesta yes Serge, it is : . Otherwise there would be exception like NoSuchBeanDefinitionException, right? – cengha Jul 07 '15 at 13:39
  • You should add a log at the begining of the `doFilter` method, and another one after the `filterChain.doFilter(...);` statement, just to control whether the exception has bean *eaten* by something else ... – Serge Ballesta Jul 07 '15 at 13:47
  • @SergeBallesta thanks, your solution worked. The problem was that in project we use grunt. grunt web.xml file was overriding webinf/web.xml. If you write your answer i can approve it. Thanks again – cengha Jul 09 '15 at 10:15

2 Answers2

1

@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 for AbstractHandlerExceptionResolver, but doc states Spring HandlerExceptionResolver implementations deal with unexpected exceptions that occur during controller execution. But Filter method should work.

To be sure of what really happens, you should add a log at the begining of the doFilter method, and another one after the filterChain.doFilter(...); statement, just to control whether the exception has bean eaten by something else, or if the problem lies in filter declaration.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

A further approach is to create a controller method with @RequestMapping(value="/error"), but I don't know what for information you will get in the method. Furthermore you can have a look at How to handle exceptions thrown while rendering a view in Spring MVC?

Community
  • 1
  • 1
niels
  • 7,321
  • 2
  • 38
  • 58