Re: [Jersey] exception handling best practices

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Wed, 24 Feb 2010 15:40:44 -0500

On Feb 24, 2010, at 3:17 PM, Chris Carrier wrote:

> So if I want to use exceptionMappers to deal with mapping to Responses
> I'd also want to do my logging there probably. In that case I'd want
> to log the input field (query parameters). Is there any access to the
> request context in the ExceptionMappers?
Yes. Like all @Provider classes you can inject any @Context type into a field or bean property setter. In your example you can just add a "@Context UriInfo uriInfo;" field to your exception mapper class and Jersey will take care of initializing it before calling the toResponse method.


> On Tue, Feb 23, 2010 at 2:41 PM, Tatu Saloranta <> wrote:
>> On Mon, Feb 22, 2010 at 12:02 PM, Craig McClanahan <> wrote:
>> ...
>>> My favorite approach is to create exception classes (extending
>>> RuntimeException or WebApplicationException) that correspond to
>>> various error conditions, then use ExceptionMapper to map them to an
>>> appropriate status code with an appropriately formatted message (with
>>> an appropriate media type). With these, I can tell a developer
>>> writing a GET method in a resource class, that when the user tries to
>>> access a resource that doesn't exist, just "throw
>>> NotFoundException(...)" and let Jersey take care of remembering that
>>> this should be a 404 with the correct media type and message format.
>> For what it's worth, that is similar to what I like to do. There's
>> limited number of exception types that are commonly needed, so
>> overhead tends to grow logarithmically; at first it may seem like lots
>> of extra work, but not so much for the long run. And it does decouple
>> things reasonably well.
>> But I agree in that there's more than one way to skin the cat,
>> -+ Tatu +-
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail: