Mickael Caruso
Mickael Caruso

Reputation: 9441

How to return HTTP 500 from ASP.NET 5 Web Api?

The controller now has function CreatedAtRoute() for 201, HttpBadRequest() for 400, etc. I don't see one for 500 which I figure would be HttpInternalServerError().

There is, however the HttpStatusCodeResult class which I can create and return:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
   ...
    try{
    }
    catch(Exception e)
    {
         return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
    }
}

But I want to return some info from e. This might be bad practice for live, but when we're testing, we'd like to see the errors from the returned body of the API call.

HttpStatusCodeResult does not have an property of type object or anything for the purpose of providing metadata.

What to do? I don't want to return a 400 because that's not what an Internal Server Error means.

Upvotes: 7

Views: 11637

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18387

return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Message describing the error here"));

or

return InternalServerError(new Exception("SOME CUSTOM MESSAGE"));

Upvotes: 5

Related Questions