Add failure logging to DiscordWebhook.cs (#30835)

* Add failure logging to DiscordWebhook.cs

Add a new function that logs errors with discord webhook's http requests.

Create, Delete, and Edit functions were modified slightly to call the log function but return the same information as before.

The log function logs the error code, caller method using a simple constant (could be better), and attempts to log headers mentioned in issue #30248.

* remove extra ';'

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* Move header error logs to debug

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Jacob
2024-08-20 14:12:30 -07:00
committed by GitHub
parent 193dcb4b45
commit beb86fa0fe

View File

@@ -68,7 +68,11 @@ public sealed class DiscordWebhook : IPostInjectInit
public async Task<HttpResponseMessage> CreateMessage(WebhookIdentifier identifier, WebhookPayload payload)
{
var url = $"{GetUrl(identifier)}?wait=true";
return await _http.PostAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
var response = await _http.PostAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
LogResponse(response, "Create");
return response;
}
/// <summary>
@@ -80,7 +84,11 @@ public sealed class DiscordWebhook : IPostInjectInit
public async Task<HttpResponseMessage> DeleteMessage(WebhookIdentifier identifier, ulong messageId)
{
var url = $"{GetUrl(identifier)}/messages/{messageId}";
return await _http.DeleteAsync(url);
var response = await _http.DeleteAsync(url);
LogResponse(response, "Delete");
return response;
}
/// <summary>
@@ -93,11 +101,40 @@ public sealed class DiscordWebhook : IPostInjectInit
public async Task<HttpResponseMessage> EditMessage(WebhookIdentifier identifier, ulong messageId, WebhookPayload payload)
{
var url = $"{GetUrl(identifier)}/messages/{messageId}";
return await _http.PatchAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
var response = await _http.PatchAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
LogResponse(response, "Edit");
return response;
}
void IPostInjectInit.PostInject()
{
_sawmill = _log.GetSawmill("DISCORD");
}
/// <summary>
/// Logs detailed information about the HTTP response received from a Discord webhook request.
/// If the response status code is non-2XX it logs the status code, relevant rate limit headers.
/// </summary>
/// <param name="response">The HTTP response received from the Discord API.</param>
/// <param name="methodName">The name (constant) of the method that initiated the webhook request (e.g., "Create", "Edit", "Delete").</param>
private void LogResponse(HttpResponseMessage response, string methodName)
{
if (!response.IsSuccessStatusCode)
{
_sawmill.Error($"Failed to {methodName} message. Status code: {response.StatusCode}.");
if (response.Headers.TryGetValues("Retry-After", out var retryAfter))
_sawmill.Debug($"Failed webhook response Retry-After: {string.Join(", ", retryAfter)}");
if (response.Headers.TryGetValues("X-RateLimit-Global", out var globalRateLimit))
_sawmill.Debug($"Failed webhook response X-RateLimit-Global: {string.Join(", ", globalRateLimit)}");
if (response.Headers.TryGetValues("X-RateLimit-Scope", out var rateLimitScope))
_sawmill.Debug($"Failed webhook response X-RateLimit-Scope: {string.Join(", ", rateLimitScope)}");
}
}
}