Skip to content

Commit

Permalink
Log payload itself when exceptions occur
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Nov 20, 2022
1 parent ac7d069 commit efae1a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/Kook.Net.Rest/KookRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class KookRestApiClient : IDisposable

private static readonly ConcurrentDictionary<string, Func<BucketIds, BucketId>> _bucketIdGenerators = new ConcurrentDictionary<string, Func<BucketIds, BucketId>>();

public event Func<HttpMethod, string, double, Task> SentRequest { add { _sentRequestEvent.Add(value); } remove { _sentRequestEvent.Remove(value); } }
public event Func<HttpMethod, string, double, Task> SentRequest { add => _sentRequestEvent.Add(value); remove => _sentRequestEvent.Remove(value); }
private readonly AsyncEvent<Func<HttpMethod, string, double, Task>> _sentRequestEvent = new AsyncEvent<Func<HttpMethod, string, double, Task>>();

protected readonly JsonSerializerOptions _serializerOptions;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -226,7 +225,7 @@ public async Task<TResponse> SendAsync<TResponse>(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<TResponse>(response);
return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync<TResponse>(response).ConfigureAwait(false);
}

internal async Task<TResponse> SendJsonAsync<TResponse>(HttpMethod method, Expression<Func<string>> endpointExpr, object payload, BucketIds ids,
Expand All @@ -242,7 +241,7 @@ public async Task<TResponse> SendJsonAsync<TResponse>(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<TResponse>(response);
return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync<TResponse>(response).ConfigureAwait(false);
}

internal Task<TResponse> SendMultipartAsync<TResponse>(HttpMethod method, Expression<Func<string>> endpointExpr, IReadOnlyDictionary<string, object> multipartArgs, BucketIds ids,
Expand All @@ -256,7 +255,7 @@ public async Task<TResponse> SendMultipartAsync<TResponse>(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<TResponse>(response);
return bypassDeserialization ? response as TResponse : await DeserializeJsonAsync<TResponse>(response).ConfigureAwait(false);
}

private async Task<Stream> SendInternalAsync(HttpMethod method, string endpoint, RestRequest request)
Expand Down Expand Up @@ -1328,9 +1327,22 @@ protected string SerializeJson(object payload)
: JsonSerializer.Serialize(payload, _serializerOptions);
}

protected T DeserializeJson<T>(Stream jsonStream)
protected async Task<T> DeserializeJsonAsync<T>(Stream jsonStream)
{
return JsonSerializer.Deserialize<T>(jsonStream, _serializerOptions);
try
{
return await JsonSerializer.DeserializeAsync<T>(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
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/KookSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit efae1a9

Please sign in to comment.