19

I have a RESTEasy service that returns a HTTP 500 when a server side error occurs. I manage to attach a body to the HTTP response in order to give more details about the error. So the response that comes out of the service looks something like this

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: application/xml;charset=ISO-8859-1
Content-Language: en-US
Content-Length: 3251
Date: Thu, 14 Oct 2010 23:22:49 GMT
Connection: close

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myErrorEnvelope><internalCode>123</internalCode><description>error details</description></myErrorEnvelope>

I have a client (spring MVC 3.0 REST client) and I am trying to capture the HTTP 500 and read the body of the response and deserialize the myErrorEnvelope object. I first catch a catch RestClientException and it correctly tells me there was a HTTP 500 response but then there seems to be no way to get the Body of the response. Is this something I'm not supposed to be able to do? Am I supposed to return the error object as the body of a HTTP 200 response instead? I really would rather return HTTP 500 with a body.

Thanks.

pastafarian
  • 1,010
  • 3
  • 16
  • 30

1 Answers1

14

You're trying to do the right thing. A framework that doesn't let you get the payload of a non-2xx response simply is broken.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
  • Is this still the case? – Eric Nov 04 '19 at 20:16
  • Public facing APIs prolly shouldn't provide any more details. But if you have an internally facing API you could save a bunch a troubleshooting time by including the error in the body. 400 is another candidate for providing additional information in the body. – Eric Labashosky Oct 27 '20 at 12:17