Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Properly throw deserializer errors #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion PocketSharp/PocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,16 @@ protected async Task<string> RequestAsString(string uri, CancellationToken cance
json = json.Replace("[]", "{}");

// deserialize object
List<PocketException> errors = new List<PocketException>();

T parsedResponse = JsonConvert.DeserializeObject<T>(
json,
new JsonSerializerSettings
{
Error = (object sender, ErrorEventArgs args) =>
{
throw new PocketException(String.Format("Parse error: {0}", args.ErrorContext.Error.Message));
errors.Add(new PocketException(String.Format("Parse error: {0}", args.ErrorContext.Error.Message), args.ErrorContext.Error));
args.ErrorContext.Handled = true;
},
Converters =
{
Expand All @@ -369,6 +372,19 @@ protected async Task<string> RequestAsString(string uri, CancellationToken cance
}
);

if (errors.Count == 1)
{
PocketException firstException = errors[0];
firstException.Data["json"] = json;
throw firstException;
}
else if (errors.Count > 1)
{
AggregateException ex = new AggregateException("Unable to parse json. See innner exceptions and exception.Data[\"json\"] for details", errors);
ex.Data["json"] = json;
throw ex;
}

return parsedResponse;
}

Expand Down