1

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?

Hashken
  • 4,396
  • 7
  • 35
  • 51
  • Perhaps because you are propagating the standard `e.getMessage ()`. Try adding a custom string to that to test. – LMC Oct 15 '18 at 22:30
  • I had set a custom string when I threw the exception in the code. I can confirm that `e.getMessage()` gives a custom string based on the print statement in the code. – Hashken Oct 16 '18 at 02:08

2 Answers2

3

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.

Konstantin Kolinko
  • 3,854
  • 1
  • 13
  • 21
1

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 ();