You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@wink.apache.org by fishrick <fi...@gmail.com> on 2010/12/21 04:35:32 UTC

How to send marshalled exception in the response body

It looks like no matter what we do, if the server resource throws an
exception (even a WebApplicationException, an error response code is sent
back to the client and the Client throws its own exception (not the one
thrown by the server resource). 

We've looked at the error handler chain and ExceptionMapper stuff and the
documentation and it is difficult to figure out if we can propogate the
actual thrown exception back to the RestClient Resource. Is it possible to
get in the mix via a handler or MessageBodyWriter and customized client to
marshall the exception into the response body and have the Resource
unmarshall it back and throw that exception back to the client?
-- 
View this message in context: http://apache-wink-users.3471013.n2.nabble.com/How-to-send-marshalled-exception-in-the-response-body-tp5854662p5854662.html
Sent from the Apache Wink Users mailing list archive at Nabble.com.

Re: How to send marshalled exception in the response body

Posted by fishrick <fi...@gmail.com>.
Thanks. I will follow your advice and go down that path.
-- 
View this message in context: http://apache-wink-users.3471013.n2.nabble.com/How-to-send-marshalled-exception-in-the-response-body-tp5854662p5860180.html
Sent from the Apache Wink Users mailing list archive at Nabble.com.

Re: How to send marshalled exception in the response body

Posted by Michael Elman <el...@apache.org>.
I don't know any easy way to do it, especially if you want to keep the
stacktrace.
If you are interested only in exception, you can do something like this:

Server Side: Implement ExceptionMapper that takes care of any exception:

public Response toResponse(Exception exception) {
    retrun
Response.serverError().entity(exception.getClass().getName()).build();
}

Client Side:
try {} catch (ClientWebException e) {
if (e.getResponse().getStatusCode() == 500) {
String className = e.getResponse().getEntity(String.class);
throw Class.forName(className).newInstance();
}
}

I didn't test the actual code, but it should give you the idea.
Note that the same exception class must be present both in client and
server.

Also, personally I really dislike the idea of passing exceptions from server
to client. It isn't very restful way.
In most cases you should rely on HTTP status codes, while you can also put
some additional hints to the body. BUT this should NOT be a java exception,
but rather some status agreed between client and server.

On Tue, Dec 21, 2010 at 5:35 AM, fishrick <fi...@gmail.com> wrote:

>
> It looks like no matter what we do, if the server resource throws an
> exception (even a WebApplicationException, an error response code is sent
> back to the client and the Client throws its own exception (not the one
> thrown by the server resource).
>
> We've looked at the error handler chain and ExceptionMapper stuff and the
> documentation and it is difficult to figure out if we can propogate the
> actual thrown exception back to the RestClient Resource. Is it possible to
> get in the mix via a handler or MessageBodyWriter and customized client to
> marshall the exception into the response body and have the Resource
> unmarshall it back and throw that exception back to the client?
> --
> View this message in context:
> http://apache-wink-users.3471013.n2.nabble.com/How-to-send-marshalled-exception-in-the-response-body-tp5854662p5854662.html
> Sent from the Apache Wink Users mailing list archive at Nabble.com.
>