You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Winnebeck, Jason" <Ja...@windstream.com> on 2013/12/13 15:03:03 UTC

JAX-RS - Capturing Unhandled Exceptions

In my application I wanted to catch any unhandled exceptions from my JAX-RS methods to customize the response. So I created a class UnhandledExceptionMapper implements ExceptionMapper<Exception> and register it as a provider. This works fine, even my other providers with more specific exceptions are used in preference. However, the UnhandledExceptionMapper also is catching WebApplicationExceptions, even those thrown by CXF itself (such as what happens with invalid media type). How can I delegate to the default behavior in a standard JAX-RS way? Rethrowing the WebApplicationException doesn't work (it gets caught by the container)?

Right now my workaround is to instantiate directly a org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper in my mapper, and if the exception caught is a WebApplicationException, delegate to the CXF. But this is not "portable", and maybe I've missed other built-in exception handlers. Is there a way I can catch "all exceptions not handled by another exception mapper" so I catch things like NullPointerException, IllegalArgumentException, etc. but not catch ones otherwise handled?

Jason Winnebeck

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.

RE: JAX-RS - Capturing Unhandled Exceptions

Posted by "Winnebeck, Jason" <Ja...@windstream.com>.
That's what I thought, until I opened up that class and saw it was 127 lines and a decent amount of code. But, on closer examination, the majority of appears to be about logging (which I do already), customizing error message (which I don't care about), and working around broken implementations that return null from getResponse (does this happen practically?).

So, in conclusion, I'll keep the delegation to CXF internal class for now but note that if we switch from CXF or the code becomes incompatible with a future CXF release, just return getResponse, it's portable and good enough.

Thanks,
Jason

> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Friday, December 13, 2013 11:23 AM
>
> I guess the most portable way is to convert this caught
> WebApplicationException into response, simply return
> caughtWebApplicationException.getResponse() from your catch-all mapper
> 
> Cheers. Sergey

----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.

Re: JAX-RS - Capturing Unhandled Exceptions

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 13/12/13 14:03, Winnebeck, Jason wrote:
> In my application I wanted to catch any unhandled exceptions from my JAX-RS methods to customize the response. So I created a class UnhandledExceptionMapper implements ExceptionMapper<Exception> and register it as a provider. This works fine, even my other providers with more specific exceptions are used in preference. However, the UnhandledExceptionMapper also is catching WebApplicationExceptions, even those thrown by CXF itself (such as what happens with invalid media type). How can I delegate to the default behavior in a standard JAX-RS way? Rethrowing the WebApplicationException doesn't work (it gets caught by the container)?
>
> Right now my workaround is to instantiate directly a org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper in my mapper, and if the exception caught is a WebApplicationException, delegate to the CXF. But this is not "portable", and maybe I've missed other built-in exception handlers. Is there a way I can catch "all exceptions not handled by another exception mapper" so I catch things like NullPointerException, IllegalArgumentException, etc. but not catch ones otherwise handled?
>
I guess the most portable way is to convert this caught 
WebApplicationException into response, simply return 
caughtWebApplicationException.getResponse() from your catch-all mapper

Cheers. Sergey
> Jason Winnebeck
>
> ----------------------------------------------------------------------
> This email message and any attachments are for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message and any attachments.
>