users@jersey.java.net

Re: error handling

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 01 Apr 2008 12:21:21 +0200

Jean Aurambault wrote:
> Hi,
>
> I have few questions regarding error handling in jersey.
>
> The main objective is to be able to return, in addition of code status,
> a description of the error. Basically I don't want to get standard
> errors from the container (eg. tomcat).
> Moreover according to the resource representation (XML or JSON)
> requested by the user, the error has to be encoded either XML or JSON.
>

As i understand it the client Accept header need only be taken into
account for successful requests. For example:

   1) what should be the representation returned for a 406 (Not
      Acceptable) response; or

   2) if a 500 response should occur but the representation is not
      acceptable should a 406 override that?

But i think it useful if capable to return the error in an acceptable
format.


> 1) The first issue I have is about the selection of the type: XML or
> JSON that will be used. Let's say I have a resource like that
> (comment/question are in the code):
>
> @GET
> @ProduceMime({"application/xml", "application/json"})
> public Response get() {
>
> if (!isValid) {
> // Is it possible here to have the ProduceMime apply to the
> entity ErrorConverter (which is jaxb bean)? throw
> new WebApplicationException(Response.status(400).entity(new
> ErrorConverter(400, "name", "description")).build());
> }
>
> // here the ProduceMime is applied and I would like to have the
> same behaviour when the error is thrown
> return Response.status(200).entity(new ProductConverter("name",
> "description")).build();
> }
>

Off the top of my head i don't think that will work at the moment. But
it should be possible to fix. (Note that it will work if you just return
Response rather than throw an exception :-) ). Could you log an issue?


> 2) The second question is about the part of the framework which
> generates exceptions (eg. jaxb unmarshalling doesn't work and throws
> exception) . I would like to have the control to return my specific
> errors and not the basic ones of the container.
> Is there a solution to do that ? If not, is it plan to have such a
> mechanism in the future ?
>

Yes, we are currently discussing it, see here:

https://jsr311.dev.java.net/servlets/BrowseList?list=dev&by=thread&from=1090206

let us know what you think. We don't any rules yet to say what happens
when an exception is thrown in a message body reader/writer.

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109