1

I am making a REST web service using restlet framework. I am using the org.restlet 2.3.1 jar

I have to customize the error response header. Depending on specific issue i have to show the response status code and status reason in response header. For this i have made a class that extends StatusService and override the toStatus method. I check the throwable and set the status accordingly. Refer sample code below.

@Override
public Status toStatus(Throwable throwable, Request request,
        Response response) {
    if (throwable != null) {
        return new Status(500,throwable, "DD","DD");
    }

    return super.toStatus(throwable, request, response);
}

So whenever exception is thrown from the restlet code it is caught here and the error response is returned. The error response has correct status code that is 500 but the status reason is not correct. Instead of DD it shows Internal Server Error.

HTTP/1.1 500 Internal Server Error

Is there some but with the restlet framework version i am using. Please help.

kaka
  • 7,425
  • 4
  • 19
  • 23

1 Answers1

0

I think that you can't change the reason phrase for predefined HTTP status codes. They are defined by the HTTP specification itself...

If you want to provide additional hints to the client, you should use the response payload and override the method toRepresentation of the status service.

Following link gives more details on how to do that: https://templth.wordpress.com/2015/02/27/exception-handling-with-restlet/.

Edited

As requested, you can also send some hints about the error message within a custom response header, as described below:

@Override
public Status toStatus(Throwable throwable, Resource resource) {
    Status status = Status.SERVER_ERROR_INTERNAL;

    Response response = resource.getResponse();

    Series<Header> responseHeaders = response.getHeaders();
    responseHeaders.add(new Header(
                 "Some-Header", "my custom message"));

    return status;

}

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Is it possible to send error message in any other http response header. I know its possible to send error message as a response body by overriding the toRepresentation. But i dont want to show the error message in http response body. I want to send the error message in http header. – kaka May 29 '15 at 12:22
  • I have small comments on Thierry's answer. You have access the response by just calling "resource.getReponse()" method. Then, you can access the headers by just calling the "Response#getHeaders()" method. In addition, I would say the generation of the reason phrase seems to be handled by the underlying HTTP connector. The internal connector (based on JDK's HttpServer class) it shows "Internal Server Error", Jetty generates a "Server Error" response phrase. – Thierry Boileau May 29 '15 at 12:57
  • I updated my answer according to Thierry's comments! – Thierry Templier May 29 '15 at 13:17