Properly handle 5xx errors from webhook calls
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
using System.Collections.Generic;
 | 
					using System.Collections.Generic;
 | 
				
			||||||
using System.IO;
 | 
					using System.IO;
 | 
				
			||||||
using System.Linq;
 | 
					using System.Linq;
 | 
				
			||||||
@@ -17,6 +18,9 @@ using Serilog;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace PluralKit.Bot
 | 
					namespace PluralKit.Bot
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    public class WebhookExecutionErrorOnDiscordsEnd: Exception {
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    public class WebhookExecutorService
 | 
					    public class WebhookExecutorService
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        private WebhookCacheService _webhookCache;
 | 
					        private WebhookCacheService _webhookCache;
 | 
				
			||||||
@@ -73,7 +77,7 @@ namespace PluralKit.Bot
 | 
				
			|||||||
            var responseString = await response.Content.ReadAsStringAsync();
 | 
					            var responseString = await response.Content.ReadAsStringAsync();
 | 
				
			||||||
            if (responseString.StartsWith("<"))
 | 
					            if (responseString.StartsWith("<"))
 | 
				
			||||||
                // if the response starts with a < it's probably a CloudFlare error or similar, so just force-break
 | 
					                // if the response starts with a < it's probably a CloudFlare error or similar, so just force-break
 | 
				
			||||||
                response.EnsureSuccessStatusCode();
 | 
					                throw new WebhookExecutionErrorOnDiscordsEnd();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var responseJson = JsonConvert.DeserializeObject<JObject>(responseString);
 | 
					            var responseJson = JsonConvert.DeserializeObject<JObject>(responseString);
 | 
				
			||||||
            if (responseJson.ContainsKey("code"))
 | 
					            if (responseJson.ContainsKey("code"))
 | 
				
			||||||
@@ -91,6 +95,11 @@ namespace PluralKit.Bot
 | 
				
			|||||||
                    throw Errors.AttachmentTooLarge; // should be caught by the check above but just makin' sure
 | 
					                    throw Errors.AttachmentTooLarge; // should be caught by the check above but just makin' sure
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // TODO: look into what this actually throws, and if this is the correct handling
 | 
					                // TODO: look into what this actually throws, and if this is the correct handling
 | 
				
			||||||
 | 
					                if ((int) response.StatusCode >= 500)
 | 
				
			||||||
 | 
					                    // If it's a 5xx error code, this is on Discord's end, so we throw an execution exception
 | 
				
			||||||
 | 
					                    throw new WebhookExecutionErrorOnDiscordsEnd();
 | 
				
			||||||
 | 
					                
 | 
				
			||||||
 | 
					                // Otherwise, this is going to throw on 4xx, and bubble up to our Sentry handler
 | 
				
			||||||
                response.EnsureSuccessStatusCode();
 | 
					                response.EnsureSuccessStatusCode();
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,9 @@ namespace PluralKit.Bot
 | 
				
			|||||||
            // Discord server errors are *not our problem*
 | 
					            // Discord server errors are *not our problem*
 | 
				
			||||||
            if (e is HttpException he && ((int) he.HttpCode) >= 500) return false;
 | 
					            if (e is HttpException he && ((int) he.HttpCode) >= 500) return false;
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 | 
					            // Webhook server errors are also *not our problem*
 | 
				
			||||||
 | 
					            if (e is WebhookExecutionErrorOnDiscordsEnd) return false;
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
            // Socket errors are *not our problem*
 | 
					            // Socket errors are *not our problem*
 | 
				
			||||||
            if (e is SocketException) return false;
 | 
					            if (e is SocketException) return false;
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user