Results 1 to 3 of 3

Thread: Exclude one exception type from @ExceptionHandler

  1. #1
    Join Date
    Apr 2012
    Posts
    17

    Default 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. #2
    Join Date
    Dec 2008
    Location
    New York City
    Posts
    135

    Default

    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.
    Andrew Thompson - Linked In

  3. #3
    Join Date
    Apr 2012
    Posts
    17

    Default

    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 at 04:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •