Question

I understand that a Jersey-based web service is able to associate exceptions thrown by service methods to desired HTTP return codes (here). Now, is there any chance to make the client generate exactly the same exception that was generated by the service method? I mean, if the server side throws MySpecificException, is there a way to store such information (i.e., the FQN of the exception class) in the HTTP response (automatically, I don't want to turn to methods that build the response explicitly, I want them to return POJOs or void), so that the client can use it to re-throw the same exception?

Was it helpful?

Solution

REST does not specify exception as a response and thus there's no straightforward way to do this (this is not RPC).

However, you can introduce your own convention. For example:

On the provider side you could define ForbiddenException:

public class ForbiddenException extends WebApplicationException {

    public ForbiddenException(String code, String readableMessage) {
        super(Response.status(Status.FORBIDDEN).entity(new ForbiddenEntity(code, readableMessage)).build());
    }

}

(You should probably compose response in ExceptionMapper instead of exception itself, but this is just for demonstration purposes).

And on the consumer side - ClientFilter:

public class ForbiddenExceptionClientFilter extends ClientFilter {

    @Override
    public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
        ClientResponse response = getNext().handle(cr);

        if (response.getStatus() == Status.FORBIDDEN.getStatusCode()) {
            ForbiddenEntity reason = response.getEntity(ForbiddenEntity.class);
            throw new RemoteResourceForbiddenException(reason.getCode(), reason.getReadableMessage());
        }

        return response;
    }

}

This works as long as server complies with the convention and client uses the client filter.

Please note, this is not "exactly the same" exception - stacktrace is not transferred, however this is the right thing to do as it does not make any sense in client application. If you need stacktrace - it should be printed to logs using ExceptionMapper on server side.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top