I have defined the following Exception Mapper to handle the custom HttpCustomException
package com.myapp.apis;
import com.myapp.apis.model.HttpCustomException;
import com.myapp.apis.model.HttpCustomExceptionStatusType;
import com.myapp.apis.model.HttpCustomNotAuthorizedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class HttpCustomExceptionMapper implements ExceptionMapper <HttpCustomException> {
@Override
public Response toResponse (HttpCustomException e) {
Response response = Response.status (new HttpCustomExceptionStatusType (Response.Status.UNAUTHORIZED, e.getMessage ())).build ();
if (e instanceof HttpCustomNotAuthorizedException) {
response = Response.status (new HttpCustomExceptionStatusType (Response.Status.INTERNAL_SERVER_ERROR, e.getMessage ())).build ();
}
System.out.println ("HttpCustomExceptionMapper: " + response.getStatus () + ", " + response.getStatusInfo ().getReasonPhrase ());
return response;
}
}
When a HttpCustomNotAuthorizedException
is thrown in the code, I can see the log message defined in the end of the toResponse
method, in catalina.out. So, the HttpCustomExceptionMapper class is being invoked during the request processing. But, in the final response, I see in the client, I only see the standard Not Authorized
message and not the custom message I set in the Response.
Why does this happen?