1

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
    }
});
4
  • You json doesn't match to TransmitModels.errorData Commented Jul 31, 2015 at 6:06
  • @EZI I would like to handle that event gracefully. Commented Jul 31, 2015 at 6:13
  • 1
    provide a sample json. Commented Jul 31, 2015 at 6:41
  • @AmitKumarGhosh As mentioned in the question the response is not valid Json, It´s just a sting e.g. "the server is dying". But I would like to catch the error gracefully. I also tryed wrapping it in a try catch but without luck. Commented Jul 31, 2015 at 7:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

THank you so much.. This was exactly what I was looking for :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.