Announcement Announcement Module
Collapse
No announcement yet.
Exclude one exception type from @ExceptionHandler Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Exclude one exception type from @ExceptionHandler

    Hi,

    we are using spring web mvc to, amongst other things, provide a restful API. Now often times it is mandated that all exceptions on the deployed web applications to be catched and a stack trace emailed to the developers.

    In case of our API we implemented this by having all controllers extend a base controller that sports a generic exception handling method like this:

    Code:
    @ExceptionHandler
    public ModelAndView handleException(HttpServletResponse response, Throwable exception) {
         // log exception
         // mail stacktrace
         // set appropriate http response code if exception is known
         // write short error message / description via MappingJacksonJsonView
         ...
    }
    There is one special case though. We use Jetty as a servlet container and in case of network timeouts, sudden browser closes etc. the exception "org.mortbay.jetty.EoFException" is thrown. There is obviously no way a webapp can handle this. Our best bet would be to re-throw the EoFException so that the servlet container can act accordingly. Unfortunately one can not simply re-throw from within an @ExceptionHandler method as the org.springframework.web.servlet.mvc.method.annotat ion.ExceptionHandlerExceptionResolver will see this as an invocation error of the handler method itself and catch the exception again.

    Now my question is whether there is a feasible way if excluding just this exception type from the handler method at all?

    Looking forward to your suggestions.

    Greetings

    a.e

  • #2
    Two comments:

    1. You can always do a check programmatically if the exception is of a certain type.
    2. Have you considered looking through the api for your logging framework for emailing exceptions? For example, I believe log4j has an smtpappender which does what you describe. But in a much less invasive way.

    Comment


    • #3
      Thank you for the input.

      1. I can check the type for sure. I can just not re-throw it programmatically as the exception handling method is executed by Spring's ExceptionHandlerExceptionResolver inside a try/catch. I have no way to escalate the jetty exception down to jetty where it belongs (and is catered for).

      2. We use SLF4J/logback. I'm sure logback may sport some SMTP-facility but I would still need to somehow "catch all" exceptions at some point to feed them into a logger, wouldn't I? Uncaught exceptions tend to go all the way down to the servlet container and usually a stack trace is rendered instead of the expected http response. This is nothing a REST API should confront it's users with.
      Last edited by a.e; Dec 4th, 2012, 04:44 AM.

      Comment

      Working...
      X