Custom HTTP reason phrase using ExceptionMapper

662 Views Asked by At

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?

2

There are 2 best solutions below

1
Konstantin Kolinko On BEST ANSWER

You should note that the current version of HTTP protocol (HTTP/2) does not support a reason phrase at all. It has been removed from the protocol.

Thus support for sending a reason phrase has been removed from Tomcat 9.0 onwards. In Tomcat 7.0, 8.5 support for sending a custom reason phrase is off by default (can be enabled with a system property, org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER). In 8.5 you also should set sendReasonPhrase="true" on a Connector.

1
Amine Aouffen On

It is showing the the standard response because you are not setting the body for your response you are only setting the status. You have to passe the custom response like this :

Response response = Response.status (Response.Status.INTERNAL_SERVER_ERROR).entity(CustomResponse).build ();

am not okay with the design, but it will be something like :

HttpCustomExceptionStatusType  status = new HttpCustomExceptionStatusType (Response.Status.INTERNAL_SERVER_ERROR, e.getMessage ())
Response response = Response.status (status).entity(status).build ();