feat(proxy): throw user-readable error when discord rejects a webhook username

This commit is contained in:
spiral 2022-09-29 17:52:28 +00:00
parent 3bae5344b6
commit 5196e332df
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 32 additions and 1 deletions

View File

@ -17,7 +17,7 @@ public class DiscordRequestException: Exception
public HttpStatusCode StatusCode => Response.StatusCode;
public int? ErrorCode => ApiError?.Code;
internal DiscordApiError? ApiError { get; init; }
public DiscordApiError? ApiError { get; init; }
public override string Message =>
(ApiError?.Message ?? Response.ReasonPhrase ?? "") + (FormError != null ? $": {FormError}" : "");

View File

@ -13,9 +13,12 @@ using Myriad.Rest.Types.Requests;
using Myriad.Types;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog;
using PluralKit.Core;
namespace PluralKit.Bot;
public class WebhookExecutionErrorOnDiscordsEnd: Exception { }
@ -148,6 +151,34 @@ public class WebhookExecutorService
webhookMessage =
await _rest.ExecuteWebhook(webhook.Id, webhook.Token, webhookReq, files, req.ThreadId);
}
catch (BadRequestException e)
{
// explanation for hacky: I don't care if this code fails, it just means it wasn't a username error
try
{
var json = JsonConvert.DeserializeObject<JObject>(e.FormError);
var error = json.Value<JObject>("username").Value<JArray>("_errors").First.Value<string>("message");
await _rest.CreateMessage(req.ChannelId, new MessageRequest {
Content = $"{Emojis.Error} Discord rejected your proxy name: {error.AsCode()}",
AllowedMentions = new AllowedMentions { Parse = {} },
});
Sentry.SentrySdk.CaptureException(e);
// this exception is ignored in the message handler lol
throw new ProxyService.ProxyChecksFailedException("_internal_discord_rejected_message");
}
catch (Exception ex)
{
// this exception is expected, see comment above
if (ex.GetType() == typeof(ProxyService.ProxyChecksFailedException))
throw ex;
else
// if something breaks, just ignore it and throw the original exception
throw e;
}
}
catch (JsonReaderException)
{
// This happens sometimes when we hit a CloudFlare error (or similar) on Discord's end