c# - How to get the exception from HttpResponseMessage in web API? -


i'm trying implement custom error handling in web api, , need exception returned httpresponsemessage.

i've tried exception info from:

response.content.readasasync<httperror>().result 

but can't access result object, i'm getting exception when trying, i'm doing wrong.

can't figure out how it, assistance appreciated.

edit:

my client code not relevant, it's 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 implemented delegatinghandler gets response, , want callstack of exception thrown in controller action above.

errordetails = new responseerrordetailsfull {     message = "an error has occurred.",     exceptionmessage = response.reasonphrase,     stacktrace = response.content.readasasync<httperror>().result.stacktrace  }; 

edit #2

ok, found out if create exceptionfilterattribute, , override onexception(), use attribute on delegatinghandler i'm able access exception mentioned in above code.

can provide explanation why working way?

to httperror in response content, server side api code needs have written httperror instance response stream.

only response.content.readasasync<httperror>().result yield data.

normally, if server side code throws exception, default behavior http 500 (internal server error) status code nothing parseable in response message.

in case of http 400 (bad request) or other such non-500 (non-200) errors, customary send response data. (either validation errors etc.) in case, may able read data response.

in general error scenario, unless server side api code not writing known type response, cannot read off response on caller side.

please post server side , client side code, further.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -