Skip to content

Commit

Permalink
Add failure logging to DiscordWebhook.cs
Browse files Browse the repository at this point in the history
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 space-wizards#30248.
  • Loading branch information
cranberriez authored Aug 9, 2024
1 parent c80c262 commit b0f349d
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions Content.Server/Discord/DiscordWebhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ public async void TryGetWebhook(string url, Action<WebhookData> onComplete)
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>
Expand All @@ -80,7 +84,11 @@ public async Task<HttpResponseMessage> CreateMessage(WebhookIdentifier identifie
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>
Expand All @@ -93,11 +101,40 @@ public async Task<HttpResponseMessage> DeleteMessage(WebhookIdentifier identifie
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.Error($"Retry-After: {string.Join(", ", retryAfter)}");

if (response.Headers.TryGetValues("X-RateLimit-Global", out var globalRateLimit))
_sawmill.Error($"X-RateLimit-Global: {string.Join(", ", globalRateLimit)}");

if (response.Headers.TryGetValues("X-RateLimit-Scope", out var rateLimitScope))
_sawmill.Error($"X-RateLimit-Scope: {string.Join(", ", rateLimitScope)}");
}
}


}

0 comments on commit b0f349d

Please sign in to comment.