users@jersey.java.net

Re: [Jersey] ExceptionMapping - 500 or still no message

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 14 Apr 2010 16:18:24 +0200

Hi Daniel,

What version of Jersey are you using?

Note that JSP error pages will only work if no response has been
written.

Would it be possible to send me a test web app exercising all the
cases you describe? that way there will be no ambiguity in what you
describe. And it will help me work out what we could do in terms of
passing information to the error page.


There is one case that may be causing some confusion. When an instance
of WebApplicationException is thrown and

- that exception is not mapped
- the status code is 500
- there is no entity present

then Jersey will modify the response to contain the stack trace as the
response entity.

         if (!mapped) {
             // Log the stack trace
             if (r.getStatus() >= 500 || request.isTracingEnabled()) {
                 traceException(e, r);
             }

             if (r.getStatus() >= 500 && r.getEntity() == null) {
                 // Write out the exception to a string
                 StringWriter sw = new StringWriter();
                 PrintWriter pw = new PrintWriter(sw);
                 e.printStackTrace(pw);
                 pw.flush();

                 r =
Response.status(r.getStatus()).entity(sw.toString()).
                         type(MediaType.TEXT_PLAIN).build();
             }
         } else if (request.isTracingEnabled()) {
             traceException(e, r);
         }

I think Jersey is in error here when it modifies the response.

Often it is not recommended to present an error page with a stack
trace for a public service as it can be a security risk. But this is
something useful for development purposes. Including the stack trace
as a response header could an option (although i dunno if this will
result in length limits being reached for headers).

Paul.


On Apr 14, 2010, at 3:38 PM, Daniel Manzke wrote:

> Hi,
>
> I developed a Service with the help of JAX-RS and I'm fighting a
> little bit with the Exception handling. I searched through the Mail-
> List and saw that there was some similiar problem and it was fixed.
> Maybe I'm just doing something wrong. ;)
>
>
> I saw that when I'm using the WebApplicationException and just
> passing a Status, I'll get the error page with the right code, but
> it's not possible to write something in the message field.
> So I created a Exception which inherits from WebApplicationException
> and wraps up my thrown Exceptions. The Stacktrace will be displayed,
> but not the Error-Page.
>
> So I did the next try with the ExceptionMapper. This will end in an
> Error-Page with 500 and the "root causes" message, but the passed
> StatusCode is ignored.
>
>
>
> What I want to do?
> - throw a Exception with a Status Code
> - display the Error-Page with the right Status Code and the Stacktrace
>
>
> Any idea?
>
>
> Thanks,
> Daniel :)