From 014999271f3ce681b23bb4b5253bca1aec019dea Mon Sep 17 00:00:00 2001 From: spiral Date: Thu, 29 Sep 2022 17:54:03 +0000 Subject: [PATCH] feat(proxy): throw user-readable error on 413 when sending proxied attachments --- Myriad/Rest/BaseRestClient.cs | 1 + .../Exceptions/DiscordRequestException.cs | 8 ++++++++ .../Services/WebhookExecutorService.cs | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/Myriad/Rest/BaseRestClient.cs b/Myriad/Rest/BaseRestClient.cs index 8877027b..1cb93a23 100644 --- a/Myriad/Rest/BaseRestClient.cs +++ b/Myriad/Rest/BaseRestClient.cs @@ -262,6 +262,7 @@ public class BaseRestClient: IAsyncDisposable HttpStatusCode.Unauthorized => new UnauthorizedException(response, body, apiError), HttpStatusCode.NotFound => new NotFoundException(response, body, apiError), HttpStatusCode.Conflict => new ConflictException(response, body, apiError), + HttpStatusCode.RequestEntityTooLarge => new RequestEntityTooLargeException(response, body, apiError), HttpStatusCode.TooManyRequests => new TooManyRequestsException(response, body, apiError), _ => new UnknownDiscordRequestException(response, body, apiError) }; diff --git a/Myriad/Rest/Exceptions/DiscordRequestException.cs b/Myriad/Rest/Exceptions/DiscordRequestException.cs index 2d09d46a..38007dd9 100644 --- a/Myriad/Rest/Exceptions/DiscordRequestException.cs +++ b/Myriad/Rest/Exceptions/DiscordRequestException.cs @@ -61,6 +61,14 @@ public class BadRequestException: DiscordRequestException { } } +public class RequestEntityTooLargeException: DiscordRequestException +{ + public RequestEntityTooLargeException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : base( + response, responseBody, apiError) + { } +} + + public class TooManyRequestsException: DiscordRequestException { public TooManyRequestsException(HttpResponseMessage response, string responseBody, DiscordApiError? apiError) : diff --git a/PluralKit.Bot/Services/WebhookExecutorService.cs b/PluralKit.Bot/Services/WebhookExecutorService.cs index ff165afa..5099e8a8 100644 --- a/PluralKit.Bot/Services/WebhookExecutorService.cs +++ b/PluralKit.Bot/Services/WebhookExecutorService.cs @@ -179,6 +179,25 @@ public class WebhookExecutorService throw e; } } + catch (RequestEntityTooLargeException e) + { + try + { + await _rest.CreateMessage(req.ChannelId, new MessageRequest { + Content = $"{Emojis.Error} One or more of the files attached to this message were not able to be proxied because they were too large.", + AllowedMentions = new AllowedMentions { Parse = {} }, + }); + + throw new ProxyService.ProxyChecksFailedException("_internal_discord_rejected_message"); + } + catch (Exception ex) + { + if (ex.GetType() == typeof(ProxyService.ProxyChecksFailedException)) + throw ex; + else + throw e; + } + } catch (JsonReaderException) { // This happens sometimes when we hit a CloudFlare error (or similar) on Discord's end