c# - WebAPI how do I deserialize exceptions on the (.NET) client side -
i have webapi 2 controller, throwing exception, , being sent client in response message. 500 internal server error, exception it's self serialised , can see json representation of in fiddler. (json pasted below)
i have been trying find way de-serialize exception .net exception object, keep getting error:
"member 'classname' not found."
currently in client trying deserialise exception via following code.
if (apiresponse.responsecode.equals(500)) // unhandled exception on server { var exceptionobject = await response.content.readasasync<exception>(); }
how de-serialize exception object on client side application? ideally should able original system.invalidoperationexception` object.
below response trying de-serialize captured in fiddler:
http/1.1 500 internal server error cache-control: no-cache pragma: no-cache content-type: application/json; charset=utf-8 expires: -1 server: microsoft-iis/10.0 x-aspnet-version: 4.0.30319 x-powered-by: asp.net date: wed, 30 sep 2015 13:43:42 gmt content-length: 1556
{"message":"an error has occurred.","exceptionmessage":"urlhelper.link must not return null.","exceptiontype":"system.invalidoperationexception","stacktrace":" @ system.web.http.results.createdatroutenegotiatedcontentresult
1.execute()\r\n @ system.web.http.results.createdatroutenegotiatedcontentresult
1.executeasync(cancellationtoken cancellationtoken)\r\n @ system.web.http.controllers.apicontrolleractioninvoker.d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.controllers.actionfilterresult.d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.filters.authorizationfilterattribute.d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.d__1.movenext()"}
thanks comment serg rogovtsev, know have not exception
httperror
. therefore, you'll have handle such:
var exceptionobject = await response.content.readasasync<system.web.http.httperror>();
i'm still not convinced that's idea, though. 500 status code means went unexpectedly wrong , there nothing there catch on server side. if have control on server, i'd suggest handle such errors there. client unable anyway, needs kind of error notification.
old answer (before comment httperror
):
a serialized exception
looks this:
{ "classname":"system.exception", "message":"some exception", "data":null, "innerexception":null, "helpurl":null, "stacktracestring":null, "remotestacktracestring":null, "remotestackindex":0, "exceptionmethod":null, "hresult":-2146233088, "source":null, "watsonbuckets":null }
what have formatted error message contains information exception.
by way: wouldn't try catch server-side exceptions on client. need handle errors on server , provide client enough information inform user happened.
Comments
Post a Comment