users@jersey.java.net

Re: [Jersey] Error handling with Jersey

From: Casper Bang <casper.bang_at_gmail.com>
Date: Wed, 16 Dec 2009 21:20:37 +0100

Paul Sandoz wrote:
>
> You would like to annotate an ExceptionMapper with @Produces ?

Not really. I would like to avoid having to write ExceptionMapper
classes and rather just annotate a method with an exception (list), an
error code and a viewable.
>
> Note that it should be possible to decide whether to return a Viewable
> entity or not by placing some logic in the toResponse method using the
> JAX-RS variant features. Or you can inject HttpContext and get the
> Content-Header set on the response.

Right. But how come building a Response with a non-200 status message
and providing a viewable entity gives an exception?

    try{
        ...
    }
    catch(SemanticException ex){
         return Response.status(400).entity(
                new Viewable("semanticError",
                ex
                )).build();
    }


SEVERE: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
        at
org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
        at
org.netbeans.modules.web.monitor.server.MonitorResponseWrapper.sendError(MonitorResponseWrapper.java:164)
        at
com.sun.jersey.spi.container.servlet.WebComponent$Writer.finish(WebComponent.java:255)


I find that odd, since Response.status(200)... and of course Response.ok()... does much the same except with a different status code. If it's only permitted to provide a String entity for 200 responses, shouldn't the build method check for this and fail?

/Casper