Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the exception from HttpResponseMessage in web API?

I'm trying to implement a custom error handling in web API, and I need to get the exception from the returned HttpResponseMessage.

I've tried to get exception info by from:

response.Content.ReadAsAsync<HttpError>().Result

But I can't access the result object, I'm getting an exception when trying, So I'm obviously doing it wrong.

Can't figure out how to do it, Assistance would be appreciated.

Edit:

My client code is not relevant, it's simply a GET request, server code:

Controller action throws Exception:

if (condition == true)
{
    var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
    {
        Content = new StringContent("Some Exception Related Message"),
        ReasonPhrase = "Some More Info"
    };

    throw new HttpResponseException(response);
}

SendAsync method of my implemented DelegatingHandler gets the response, and this is where I want to get the callstack of the exception that was thrown in the controller action above.

errorDetails = new ResponseErrorDetailsFull
{
    Message = "An error has occurred.",
    ExceptionMessage = response.ReasonPhrase,
    StackTrace = response.Content.ReadAsAsync<HttpError>().Result.StackTrace 
};

Edit #2

Ok, So I found out that if I create a ExceptionFilterAttribute, and Override OnException(), use the attribute on my DelegatingHandler I'm able to access the exception as mentioned in the above code.

Can someone provide an explanation why this is working this way?

like image 824
Y.S Avatar asked Jun 04 '15 06:06

Y.S


People also ask

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 do I throw an exception from Web API?

What you have in your code snippet should work. The server will send back a 404 Not Found to the client if test is null with no response body. If you want a response body, you should consider using Request. CreateErrorResponse as explained in the blog post above and passing that response to the HttpResponseException .

How do you handle exceptions in 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.


1 Answers

To get HttpError in the response content, your server side API code needs to have written an HttpError instance into the response stream.

Only then response.Content.ReadAsAsync<HttpError>().Result would yield that data.

normally, if a server side code throws an exception, the default behavior is an HTTP 500 (Internal Server Error) status code with nothing parseable in the response message.

In case of HTTP 400 (Bad Request) or other such non-500 (non-200) errors, it is customary to send back response data. (either Validation Errors etc.) in that case, you may be able to read the data from the response.

in general for any error scenario, unless your server side API code is not writing a known type into the response, you cannot read it off the response on the caller side.

please post your server side and client side code, for us to help you further.

like image 52
Raja Nadar Avatar answered Sep 28 '22 00:09

Raja Nadar