users@jersey.java.net

Re: [Jersey] Error handling with Jersey

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 16 Dec 2009 13:46:13 -0500

On Dec 15, 2009, at 9:54 PM, Casper Bang wrote:

<snip/>
>
> So I guess I was wondering, why require global providers of
> ExceptionMapper rather than using annotations? Something along the lines
> of the following:
>
> @GET
> @Path("/myResource")
> @ExceptionHandler(value=AuthorizationException.class, statusCode=401)
> class MyResource(){
> // Methods which throws AuthorizationExceptions
> }
>
> Which would attach an exception handler to each and every method of
> MyResource, and map AuthorizationException to a 401. The beauty of this
> is that it could union several exceptions (a la Coin's multi-catch), it
> could be used on a specific method and it could even include a viewable
> reference.
>
> @GET
> @Path("/myMethod")
> @ExceptionHandler(value={IOException.class,
> NullPointerException.class}, statusCode=500, "internalError")
> public Viewable myMethod() throws IOException{
> ...
> }
>
> Anyway, just some ideas. If I am missing something about Jersey error
> handling, kindly guide me to the docs.
>
What component/class does your @ExceptionHandler hand the job of mapping to ? IOW do you envision a local (vs global) equivalent of an ExceptionMapper ? If so wouldn't that also need to be listed in the annotation so the framework knows which one to use ?

I've found global exception mappers (implements ExceptionMapper) very useful in my work but that is a fairly uniform API with a small set of throwables. I can see it might be useful to be able to scope exception mappers such that you can select amongst multiple different mappers for the same exception depending on context - is that the kind of thing you have in mind ?

Marc.