users@jersey.java.net

[Jersey] Unmappable exceptions in server runtime

From: Dave Trombley <dave.trombley_at_gmail.com>
Date: Fri, 10 Apr 2015 11:44:42 -0400

Hi all -

    I'm having a problem with not being able to see/handle certain
exceptions being thrown in the runtime in Jersey 2.16, using a custom
container (jetty wired manually to EL and Jersey). In particular, this
occurs whenever a WebApplicationException is generated during response
writing - it is caught at ServerRuntime:683. It's then re-thrown, not
wrapped in MappableException, caught by ServerRuntime:699, and subsequently
processed.

    The RequestProcessingContext used at this point has a null
monitoringEventListener, even though I have an ApplicationEventListener
registered which has already gotten a START event - it never gets
ON_EXCEPTION (vis. ServerRuntime:446). My registered filters do get called
on this "automatically mapped" 500 response, but I cannot see any good way
to get the original exception from the arguments of the filter's observer
pattern arguments, and modifying the response in this way is not "natural"
- I want to handle this by mapping WebApplicationException to a custom 500.

    As it is I have to use the debugger to even see the detail message of
the exception, since there is just jersey's default "500" message with no
information in it. The trace headers are also unhelpful, they do not show
the exception. Moreover, the client expects a JSON object, always, and
the "default 500" is breaking it, so I need to handle unknown/uncaught
WebApplicationException like this.

   Can anyone point me in the right direction here? I've tried every
combination of listeners, filters, etc that I could think of, without
success... a typical such WebApplicationException is a wrapped
JAXBException thrown from Moxy with detail message

"Exception [EclipseLink-60] (Eclipse Persistence Services -
2.5.0.v20130507-3faac2b):
org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [setRootNode] or [getRootNode] is not
defined in the object"

  (Note: I know how to fix the particular problem here, that's not what I'm
asking about - I want to be able to map these Exception in general, and not
see that default "500" but send a customized, JSON response to the client
with the original Exception message in it.)

Thanks!
   -David