In my test application I want to catch the deserialization exception if the request body contents not either valid XML or Json. Currently when I pass invalid XML/Json in the body I am not seeing any exception, instead getting HttpStatus 200. But while debugging
I am seeing the object is null as the deserializtion did not work properly. I have a custom global exception filter, but it seems like it is not intercepting that exception.
I found the answer myself. Custom global exception handler only works if the exception is thrown from any of the controllers in the Web API. But if the exception is thrown before the controller is invoked in the web api processing pipeline, it will not get
captured. The work around I found is that in my custom DelegatingHandler(that I am using for logging), I am manually validating XML or Json format based on content-type header.
callmedev
0 Points
6 Posts
Catching Deserilization Exception
Jan 09, 2013 06:16 PM|LINK
Hi,
In my test application I want to catch the deserialization exception if the request body contents not either valid XML or Json. Currently when I pass invalid XML/Json in the body I am not seeing any exception, instead getting HttpStatus 200. But while debugging I am seeing the object is null as the deserializtion did not work properly. I have a custom global exception filter, but it seems like it is not intercepting that exception.
Thanks
abamboria
Member
145 Points
88 Posts
Re: Catching Deserilization Exception
Jan 11, 2013 12:18 PM|LINK
Can you post your code.
Mark as Answer:- if the post helped you...
callmedev
0 Points
6 Posts
Re: Catching Deserilization Exception
Jan 11, 2013 10:37 PM|LINK
Hi,
I found the answer myself. Custom global exception handler only works if the exception is thrown from any of the controllers in the Web API. But if the exception is thrown before the controller is invoked in the web api processing pipeline, it will not get captured. The work around I found is that in my custom DelegatingHandler(that I am using for logging), I am manually validating XML or Json format based on content-type header.
Thanks