From 3445209af9ea4ed1331ea43c7c6e39f2b84baa77 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 30 Mar 2024 10:08:51 +1100 Subject: [PATCH] Fix Discord bot not handling http error responses properly when running on modern dotnet versions --- .../Modules/Relay/Discord/DiscordApiClient.cs | 68 ++++++++++--------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/MCGalaxy/Modules/Relay/Discord/DiscordApiClient.cs b/MCGalaxy/Modules/Relay/Discord/DiscordApiClient.cs index 924a80349..18ee15e72 100644 --- a/MCGalaxy/Modules/Relay/Discord/DiscordApiClient.cs +++ b/MCGalaxy/Modules/Relay/Discord/DiscordApiClient.cs @@ -70,40 +70,10 @@ namespace MCGalaxy.Modules.Relay.Discord msg.ProcessResponse(resp); break; } catch (WebException ex) { - string err = HttpUtil.GetErrorResponse(ex); + bool canRetry = HandleErrorResponse(ex, msg, retry); HttpUtil.DisposeErrorResponse(ex); - HttpStatusCode status = GetStatus(ex); - // 429 errors simply require retrying after sleeping for a bit - if (status == (HttpStatusCode)429) { - SleepForRetryPeriod(ex.Response); - continue; - } - - // 500 errors might be temporary Discord outage, so still retry a few times - if (status >= (HttpStatusCode)500 && status <= (HttpStatusCode)504) { - LogWarning(ex); - LogResponse(err); - if (retry >= 2) return; - continue; - } - - // If unable to reach Discord at all, immediately give up - if (ex.Status == WebExceptionStatus.NameResolutionFailure) { - LogWarning(ex); - return; - } - - // May be caused by connection dropout/reset, so still retry a few times - if (ex.InnerException is IOException) { - LogWarning(ex); - if (retry >= 2) return; - continue; - } - - LogError(ex, msg); - LogResponse(err); - return; + if (!canRetry) return; } catch (Exception ex) { LogError(ex, msg); return; @@ -115,6 +85,40 @@ namespace MCGalaxy.Modules.Relay.Discord if (remaining == "1") SleepForRetryPeriod(res); } + static bool HandleErrorResponse(WebException ex, DiscordApiMessage msg, int retry) { + string err = HttpUtil.GetErrorResponse(ex); + HttpStatusCode status = GetStatus(ex); + + // 429 errors simply require retrying after sleeping for a bit + if (status == (HttpStatusCode)429) { + SleepForRetryPeriod(ex.Response); + return true; + } + + // 500 errors might be temporary Discord outage, so still retry a few times + if (status >= (HttpStatusCode)500 && status <= (HttpStatusCode)504) { + LogWarning(ex); + LogResponse(err); + return retry < 2; + } + + // If unable to reach Discord at all, immediately give up + if (ex.Status == WebExceptionStatus.NameResolutionFailure) { + LogWarning(ex); + return false; + } + + // May be caused by connection dropout/reset, so still retry a few times + if (ex.InnerException is IOException) { + LogWarning(ex); + return retry < 2; + } + + LogError(ex, msg); + LogResponse(err); + return false; + } + static HttpStatusCode GetStatus(WebException ex) { if (ex.Response == null) return 0;