users@jersey.java.net

Re: [Jersey] Error Reporting through HTTP Faults

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 02 Mar 2010 09:33:26 -0500

On Mar 2, 2010, at 3:20 AM, Suchitha Koneru (sukoneru) wrote:

> Hello Jersey Users ,
> I am using JDK 1.5 and Jersey version 1.1.1-ea for restful service development. We have a Flex client which consumes the restful services. I have used the Jersey exception framework (WebApplicationException) to propagate exceptions to Flex client.
>
> The exceptions thrown from the application are propagated as HTTP faults with the status code 500. If I want to include additional information(for example application error code) as a part of the propagated exception , is there a way this can be achieved ?

You need to set the Response encapsulated in the WebApplicationException, e.g. by using this constructor:

https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/WebApplicationException.html#WebApplicationException(java.lang.Throwable,%20javax.ws.rs.core.Response)

The Response should contain the desired status code and a body that contains any additional information you want to send to the client.

An alternative to using WebApplicationException as below is to provide an ExceptionMapper<CustomException>:

https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/ext/ExceptionMapper.html

The ExceptionMapper will be called to convert a CustomException into a Response so your resource methods can just throw CustomException directly.

Marc.

>
> In the code below “CustomException” is the custom exception framework used in our application. Can the “applicationErrCode” (defined below) be propagated to the client as a part of HTTP Fault.
>
>
>
>
>
> public class RestfulServiceException extends WebApplicationException implements CustomException {
>
> private static final long serialVersionUID = 1387449832483248304L;
>
> public String message;
>
> public Response.Status httpStatus;
>
> /**
> * Here the err code should follow the format
> */
> public String applicationErrCode;
>
> public String action;
>
> public String severity;
>
> public String errorCause;
>
>
> public RestfulServiceException (){
> super(500);
> httpStatus = Response.Status.INTERNAL_SERVER_ERROR;
> ;
> }
>
> public RestfulServiceException (CustomException cse){
>
> this.action= cse.getAction();
> this.applicationErrCode = cse.getErrorCode();
> this.errorCause = cse.getErrorCause();
> this.httpStatus = Response.Status.INTERNAL_SERVER_ERROR ;
> this.message = cse.getMessage();
> this.severity = cse.getSeverity();
> }
>
> Thank you,
> Suchitha.