users@jersey.java.net

[Jersey] Observing exceptions thrown by resources

From: Marshall Pierce <marshall_at_mpierce.org>
Date: Wed, 1 May 2013 09:24:06 -0700

I'd like to be able to observe all exceptions thrown by resource methods so that I can feed them into our metrics gathering service.

Some googling leads me to the following conclusions:

- If I use a ContainerResponseFilter, then I will not be able to access exceptions (via getMappedThrowable) if there is no exception mapper that handles them. [1] The fact that other filters can throw exceptions to disrupt this isn't awesome but I can live with it.

- If I use a servlet filter, then I will not have visibility to any subclass of WebApplicationException because Jersey handles those.

Ideally, what I'd like to do is be able to provide an implementation of something like this that would expose both WebApplicationExceptions as well as any other exception thrown by a resource method:

interface ThrowableObserver {
    void onThrowable(Throwable t);
}

Another way would be to access unmapped exceptions in a container response filter. I'm hoping that functionality like this exists and I just didn't find it… :)

-Marshall

[1] http://markmail.org/thread/orcctyu7hczx3d5w#query:+page:1+mid:zczy5mgucn5qoolt+state:results