Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error messages returned from Web API method are omitted in non-dev environment

I have a Web API controller POST method that behaves fine locally and on the testing server. If everything goes well it returns:

new HttpResponseMessage( HttpStatusCode.Created ) 

If something goes wrong, it returns:

new HttpResponseMessage<IEnumerable<string>>( usefulMessages, HttpStatusCode.BadRequest ); 

The problem is that when I make a request to the testing server that results in an error, I get the bad request code back but I never see the messages. If I make the exact same request to my local machine, I do see the messages. The following outputs are from my own tool:

Sending a request to my local machine I get:

Status code: 400 (BadRequest) Response data: ["Error message one", "Error message two"] 

Sending a request to the testing server I get:

Status code: 400 (BadRequest) Response data: Bad Request 

The code that is running is exactly the same. The database is the same. Everything is the same except for the server servicing the request. I even have code to email myself the error messages so I know that the server is producing the correct error messages and behaving correctly. Could this be an IIS thing (like the equivalent of customErrors = RemoteOnly for Web API)? Not only are the error messages omitted from the response data, something invents the phrase "Bad Request" to put in there instead.

Any ideas? Thanks.

like image 812
Greg Smalter Avatar asked Apr 20 '12 13:04

Greg Smalter


People also ask

How can you handle errors in Web API?

You can customize how Web API handles exceptions by writing an exception filter. An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception.

How does Web API handle 400 error?

BadRequest: 400 Error A 400 error, BadRequest, is used when you have validation errors from data posted back by the user. You pass a ModelStateDictionary object to the BadRequest method and it converts that dictionary into JSON which, in turn, is passed back to your HTML page.

How does Web API handle validation errors?

Handling Validation Errors Web API does not automatically return an error to the client when validation fails. It is up to the controller action to check the model state and respond appropriately. If model validation fails, this filter returns an HTTP response that contains the validation errors.


1 Answers

Take a look at this MSDN post on the HttpConfiguration.IncludesErrorDetailPolicy:

In your Global.asax:

var config = GlobalConfiguration.Configuration; config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 

I have used this configuration property to force error messages to include details.

like image 55
syneptody Avatar answered Sep 20 '22 08:09

syneptody