Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 410
Mickael Caruso Avatar asked Dec 10 '15 16:12

Mickael Caruso


1 Answers

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

or

return InternalServerError(new Exception("SOME CUSTOM MESSAGE"));
like image 183
Thiago Custodio Avatar answered Nov 12 '22 10:11

Thiago Custodio