feat(proxy): throw user-readable error when discord rejects a webhook username
This commit is contained in:
		@@ -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}" : "");
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user