users@jsr311.java.net

Re: Telling ExceptionMapper to exclude exceptions

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Fri, 10 Oct 2008 10:11:19 -0700

cowwoc wrote:
> Hi Marc,
>
> I'd like to process all uncaught RuntimeExceptions thrown by
> resource methods. I'm simply trying to ensure that resource methods
> returns a valid response even if the code forgets to catch an exception.
In my application, I'm actually taking the opposite approach -- my
resource method throws an exception for an "exceptional condition" :-),
and I then rely on an exception mapper to do the right thing. For
example, let's say I want to return a 404 Not Found when the client
presents an id that is not found.

    @Path("/customers")
    public class CustomersResource {
        ...
        @GET
        @Path("{id}")
        public Response find(@PathParam("id") String id) {
            Customer customer = MyBusinessLogic.findCustomer(id);
            if (customer != null) {
                return Response.ok(customer).build();
            } else {
                throw new NotFoundException();
            }
        }
    }

And I have an exception mapper for NotFoundException that emits a 404.
This has seemed pretty elegant so far. In particular, it avoids the
need for the developer creating resource classes from having to remember
"now which HTTP status do I use for this particular use case" and
sometimes getting it wrong.

I supply exception mappers for all the exceptional conditions that I
care about at the application level (AuthenticationException -> 401,
AuthorizationException -> 403, and so on). If there's no such mapper,
you end up with a 500, which seems like the correct behavior.

Craig

> Now, I could add try{}catch blocks on a per-method basis one by one
> but it seems like there should be a better mechanism for doing this
> that is easier to maintain. And besides, if I forgot to catch some
> exception in the first place it seems equally likely that I forgot to
> surround the code with a try{}catch block.
>
> Gili
>
>> On Oct 3, 2008, at 8:35 AM, cowwoc wrote:
>>>
>>> We need a portable mechanism to tell ExceptionMapper to catch all
>>> application-generated RuntimeExceptions but pass-through all
>>> exceptions generated by the underlying JAX-RS implementation (meant
>>> to be handled by the container, not our code). I suspect this is a
>>> very common use-case but there doesn't seem to be a way to do it.
>>>
>> Can you say a bit more about what you are trying to achieve. Only
>> certain exceptions are required to be processed by exception mappers:
>>
>> - those thrown by a resource method
>> - those thrown by a provider method
>> - those thrown by the ctor or valueOf method of a resource field,
>> property or method parameter
>>
>> It sounds like you want to be able to distinguish between say an
>> IllegalArgumentException thrown by UriBuilder vs one thrown by some
>> other API that a resource method uses but I'm struggling to see a use
>> case for that.
>>
>> Thanks,
>> Marc.
>>
>> ---
>> Marc Hadley <marc.hadley at sun.com>
>> CTO Office, Sun Microsystems.
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jsr311.dev.java.net
> For additional commands, e-mail: users-help_at_jsr311.dev.java.net
>