Fix NRE on http error handling (#1970)

In some cases, the code is not able to deserialize API.DiscordError, but
it attempts to use its message to throw an exception. This change uses
null as the exception reason in that case.
This commit is contained in:
Adam Gauthier
2021-12-18 01:21:33 -08:00
committed by GitHub
parent c24b7a145a
commit 52e2019990

View File

@@ -1,6 +1,4 @@
using Discord.API;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
#if DEBUG_LIMITS #if DEBUG_LIMITS
using System.Diagnostics; using System.Diagnostics;
@@ -106,17 +104,22 @@ namespace Discord.Net.Queue
{ {
try try
{ {
using (var reader = new StreamReader(response.Stream)) using var reader = new StreamReader(response.Stream);
using (var jsonReader = new JsonTextReader(reader)) using var jsonReader = new JsonTextReader(reader);
{
error = Discord.Rest.DiscordRestClient.Serializer.Deserialize<API.DiscordError>(jsonReader); error = Discord.Rest.DiscordRestClient.Serializer.Deserialize<API.DiscordError>(jsonReader);
}
} }
catch { } catch { }
} }
throw new HttpException(response.StatusCode, request, error?.Code, error.Message, error.Errors.IsSpecified throw new HttpException(
? error.Errors.Value.Select(x => new DiscordJsonError(x.Name.GetValueOrDefault("root"), x.Errors.Select(y => new DiscordError(y.Code, y.Message)).ToArray())).ToArray() response.StatusCode,
: null); request,
error?.Code,
error?.Message,
error?.Errors.IsSpecified == true ?
error.Errors.Value.Select(x => new DiscordJsonError(x.Name.GetValueOrDefault("root"), x.Errors.Select(y => new DiscordError(y.Code, y.Message)).ToArray())).ToArray() :
null
);
} }
} }
else else