users@jersey.java.net

Re: [Jersey] Returning error messages

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 02 Oct 2008 09:16:40 +0100

On Oct 2, 2008, at 8:23 AM, tarjei wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi,
> Paul Sandoz wrote:
>>
>> On Oct 1, 2008, at 10:21 AM, tarjei wrote:
>>
>> Hi, is there a nice way to return error messages together with an
>> error
>> code?
>>
>> Usecase: If I return 400 Bad Request to a client, it would help the
>> client a lot if the response also contained a hint on what the
>> service
>> thought was wrong (for example validation errors).
>>
>>
>>> When you say "error message" do you mean a message that is sent as
>>> the
>>> body of the response or do you mean a message that is part of the
>>> response status line?
>>
>>> The latter is not supported. For the former you can use
>>> Response.ResposneBuilder [1], perhaps in conjunction with
>>> WebApplicationException [2], or mapping exceptions to responses
>>> using
>>> ExceptionMapper [3] to set the body of the response.
>
> Thanks, I figured it out in the end and ended up with:
>
> Response r = Response.ok("Error:" +
> ex.getMessage()).status(501).build();
> throw new WebApplicationException(r);
>

A slightly better way is as follows:

   Response r = Response.status(501).entity("Error:" +
ex.getMessage().build();
   throw new WebApplicationException(r);

Paul.

> I should probably wrap it in xml or something, but this works for now.
> Of course, in the example above, I could also just wrap the original
> exception in WebApplicationException instead of building a response,
> but
> I'm planning to use this for other purposes as well.
>
> Regards,
> Tarjei
>>
>>> Paul.
>>
>>> [1]
>>> https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/Response.ResponseBuilder.html
>>
>>> [2]
>>> https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/WebApplicationException.html
>>
>>> [3]
>>> https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/ext/ExceptionMapper.html
>>
>>
>> Regards,
>> Tarjei
>>>
> -
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFI5HcEYVRKCnSvzfIRAuhrAKCe74OSPx0uUSjw1iBfpsvwAk/42ACfV7Jr
> P2kuqtaB3rB0VSMvHvE8Xkg=
> =Hd+t
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>