c# - Handle Deserialization Exception in Formatter of Delegating Handler -
i'm using web api , implementing delegating handler.
i have customization of json serializer / deserializer registered formatter in api configuration.
var globalformatters = globalconfiguration.configuration.formatters; var jsonformatter = globalformatters.jsonformatter; jsonformatter.serializersettings.formatting = newtonsoft.json.formatting.indented; jsonformatter.serializersettings.converters.add(...)
as exception handling, i've added exceptionfilterattribute, , added filter in configuration.
public class methodattributeexceptionhandling : exceptionfilterattribute { public override void onexception(httpactionexecutedcontext actionexecutedcontext) { var errorhandler = new errorhandler(); var response = errorhandler.processerror(actionexecutedcontext); actionexecutedcontext.response = response; } }
this seemed working well, until encountered deserialization exception, did not caught in filter.
i've read exception handling documentation here mentions serialization exception not caught filter (it not mention serialization however), , couldn't find solution catch , handle properly.
in case else,
in order deal issue implemented actionfilterattribute, in implementation of onactionexecuting i'm checking if model state invalid, , error handling stuff if so.
that's spot before action execution , after object deserialization.
public class validatemodelattribute : actionfilterattribute { public override void onactionexecuting(httpactioncontext actioncontext) { if (actioncontext.modelstate.isvalid) { return; } //do error handling stuff... } }
Comments
Post a Comment