feat(proxy): throw user-readable error on 413 when sending proxied attachments

This commit is contained in:
spiral 2022-09-29 17:54:03 +00:00
parent 5196e332df
commit 014999271f
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
3 changed files with 28 additions and 0 deletions

View File

@ -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)
};

View File

@ -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) :

View File

@ -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