From efae1a96a837a97e0490bbc650d86b3ae8aefd7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=88=88=E5=B0=8F=E8=8D=B7?= Date: Sun, 20 Nov 2022 19:35:21 +0800 Subject: [PATCH] Log payload itself when exceptions occur --- src/Kook.Net.Rest/KookRestApiClient.cs | 26 ++++++++++++++++------ src/Kook.Net.WebSocket/KookSocketClient.cs | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Kook.Net.Rest/KookRestApiClient.cs b/src/Kook.Net.Rest/KookRestApiClient.cs index f4726ed9..d5b0d45b 100644 --- a/src/Kook.Net.Rest/KookRestApiClient.cs +++ b/src/Kook.Net.Rest/KookRestApiClient.cs @@ -26,7 +26,7 @@ internal class KookRestApiClient : IDisposable private static readonly ConcurrentDictionary> _bucketIdGenerators = new ConcurrentDictionary>(); - public event Func SentRequest { add { _sentRequestEvent.Add(value); } remove { _sentRequestEvent.Remove(value); } } + public event Func SentRequest { add => _sentRequestEvent.Add(value); remove => _sentRequestEvent.Remove(value); } private readonly AsyncEvent> _sentRequestEvent = new AsyncEvent>(); protected readonly JsonSerializerOptions _serializerOptions; @@ -58,7 +58,6 @@ public KookRestApiClient(RestClientProvider restClientProvider, string userAgent Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, NumberHandling = JsonNumberHandling.AllowReadingFromString }; - // SerializerOptions.Converters.Add(new EmbedConverter()); DefaultRatelimitCallback = defaultRatelimitCallback; RequestQueue = new RequestQueue(); @@ -226,7 +225,7 @@ public async Task SendAsync(HttpMethod method, string endp var request = new RestRequest(RestClient, method, endpoint, options); Stream response = await SendInternalAsync(method, endpoint, request).ConfigureAwait(false); - return bypassDeserialization ? response as TResponse : DeserializeJson(response); + return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync(response).ConfigureAwait(false); } internal async Task SendJsonAsync(HttpMethod method, Expression> endpointExpr, object payload, BucketIds ids, @@ -242,7 +241,7 @@ public async Task SendJsonAsync(HttpMethod method, string var request = new JsonRestRequest(RestClient, method, endpoint, json, options); Stream response = await SendInternalAsync(method, endpoint, request).ConfigureAwait(false); - return bypassDeserialization ? response as TResponse : DeserializeJson(response); + return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync(response).ConfigureAwait(false); } internal Task SendMultipartAsync(HttpMethod method, Expression> endpointExpr, IReadOnlyDictionary multipartArgs, BucketIds ids, @@ -256,7 +255,7 @@ public async Task SendMultipartAsync(HttpMethod method, st var request = new MultipartRestRequest(RestClient, method, endpoint, multipartArgs, options); Stream response = await SendInternalAsync(method, endpoint, request).ConfigureAwait(false); - return bypassDeserialization ? response as TResponse : DeserializeJson(response); + return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync(response).ConfigureAwait(false); } private async Task SendInternalAsync(HttpMethod method, string endpoint, RestRequest request) @@ -1328,9 +1327,22 @@ protected string SerializeJson(object payload) : JsonSerializer.Serialize(payload, _serializerOptions); } - protected T DeserializeJson(Stream jsonStream) + protected async Task DeserializeJsonAsync(Stream jsonStream) { - return JsonSerializer.Deserialize(jsonStream, _serializerOptions); + try + { + return await JsonSerializer.DeserializeAsync(jsonStream, _serializerOptions).ConfigureAwait(false); + } + catch (JsonException ex) + { + if (jsonStream is MemoryStream memoryStream) + { + string json = Encoding.UTF8.GetString(memoryStream.ToArray()); + throw new JsonException($"Failed to deserialize JSON to type {typeof(T).FullName}\nJSON: {json}", ex); + } + + throw; + } } internal class BucketIds diff --git a/src/Kook.Net.WebSocket/KookSocketClient.cs b/src/Kook.Net.WebSocket/KookSocketClient.cs index 28786110..f890c4eb 100644 --- a/src/Kook.Net.WebSocket/KookSocketClient.cs +++ b/src/Kook.Net.WebSocket/KookSocketClient.cs @@ -1637,7 +1637,7 @@ await _gatewayLogger.WarningAsync($"Unknown Socket Frame Type ({gatewaySocketFra } catch (Exception ex) { - await _gatewayLogger.ErrorAsync($"Error handling {gatewaySocketFrameType}", ex).ConfigureAwait(false); + await _gatewayLogger.ErrorAsync($"Error handling {gatewaySocketFrameType}. Payload: {payload}", ex).ConfigureAwait(false); } }