Search code examples
javaweb-servicesjax-rshttp-response-codes

Get the status from ClientErrorException


About half of the many constructors of ClientErrorException take a Response.Status. But why is there no way of getting it back? Doesn't it have any purpose other than being validated in javax.ws.rs.WebApplicationException#validate(javax.ws.rs.core.Response, javax.ws.rs.core.Response.Status.Family) ?

I thought the way of using it would be extending it

public class WhateverInvalidInRequestException extends ClientErrorException {

    public WhateverInvalidInRequestException (String message) {
        super(message, Response.Status.BAD_REQUEST);
    }
}

And then use a single ExceptionMapper for all the subclasses, which is very convenient

@Provider
public class ClientErrorExceptionMapper implements ExceptionMapper<ClientErrorException> {

    @Override
    public Response toResponse(ClientErrorException exception) {
        return Response.status(exception.getStatus()).entity(exception.getMessage()).build();
    }
}

But there is no such a thing as exception.getStatus().

What am I missing?


Solution

  • You can get it this way:

    exception.getResponse().getStatus()