简体   繁体   中英

Json.NET error handling fails when serializing to specific object

I'm trying to use JsonSerializerSettings for custom error handling, but when I specify the object type the error stops in runtime debugging. The "json" is no valid JSON, due to remote error that is out of my hands to change/fix.

Working:

var responseData = JsonConvert.DeserializeObject(json,new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore,
    Error = (sender, args) =>
    {
      // My error handling
    }
});

Breaks With:

Additional information: Error converting value "Received merchantid does not match a registered merchant" to type 'TransmitModels+errorData'. Path ...

TransmitModels.errorData responseData = JsonConvert.DeserializeObject<TransmitModels.errorData>(json,new JsonSerializerSettings
{
    MissingMemberHandling = MissingMemberHandling.Ignore,
    Error = (sender, args) =>
    {
      // My error handling
    }
});

You need to call

args.ErrorContext.Handled = true;

in your callback to tell Json.NET that you handled the exception. If you don't (maybe because you just want to log the error), the exception is thrown after your callback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM