From ee3ac446643cd49e6b447bebd040cf7cbee5f2cc Mon Sep 17 00:00:00 2001 From: Ske Date: Wed, 26 Feb 2020 19:47:30 +0100 Subject: [PATCH] Properly handle 5xx errors from webhook calls --- PluralKit.Bot/Services/WebhookExecutorService.cs | 11 ++++++++++- PluralKit.Bot/Utils/MiscUtils.cs | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index cfbd4155..ecfb43a5 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -17,6 +18,9 @@ using Serilog; namespace PluralKit.Bot { + public class WebhookExecutionErrorOnDiscordsEnd: Exception { + } + public class WebhookExecutorService { private WebhookCacheService _webhookCache; @@ -73,7 +77,7 @@ namespace PluralKit.Bot var responseString = await response.Content.ReadAsStringAsync(); if (responseString.StartsWith("<")) // if the response starts with a < it's probably a CloudFlare error or similar, so just force-break - response.EnsureSuccessStatusCode(); + throw new WebhookExecutionErrorOnDiscordsEnd(); var responseJson = JsonConvert.DeserializeObject(responseString); if (responseJson.ContainsKey("code")) @@ -91,6 +95,11 @@ namespace PluralKit.Bot throw Errors.AttachmentTooLarge; // should be caught by the check above but just makin' sure // TODO: look into what this actually throws, and if this is the correct handling + if ((int) response.StatusCode >= 500) + // If it's a 5xx error code, this is on Discord's end, so we throw an execution exception + throw new WebhookExecutionErrorOnDiscordsEnd(); + + // Otherwise, this is going to throw on 4xx, and bubble up to our Sentry handler response.EnsureSuccessStatusCode(); } diff --git a/PluralKit.Bot/Utils/MiscUtils.cs b/PluralKit.Bot/Utils/MiscUtils.cs index 07520144..68cbe613 100644 --- a/PluralKit.Bot/Utils/MiscUtils.cs +++ b/PluralKit.Bot/Utils/MiscUtils.cs @@ -20,6 +20,9 @@ namespace PluralKit.Bot // Discord server errors are *not our problem* if (e is HttpException he && ((int) he.HttpCode) >= 500) return false; + // Webhook server errors are also *not our problem* + if (e is WebhookExecutionErrorOnDiscordsEnd) return false; + // Socket errors are *not our problem* if (e is SocketException) return false;