From eb05cbf76c7a83b3ffac656914ecf1362debb8bd Mon Sep 17 00:00:00 2001 From: spiral Date: Tue, 12 Oct 2021 08:34:28 -0400 Subject: [PATCH] feat(apiv2): ignore exception caused by invalid user-provided JSON return 400 bad request instead --- PluralKit.API/Startup.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/PluralKit.API/Startup.cs b/PluralKit.API/Startup.cs index f14dafba..67626ffa 100644 --- a/PluralKit.API/Startup.cs +++ b/PluralKit.API/Startup.cs @@ -133,6 +133,15 @@ namespace PluralKit.API app.UseExceptionHandler(handler => handler.Run(async ctx => { var exc = ctx.Features.Get(); + + // handle common ISEs that are generated by invalid user input + if (exc.Error is InvalidCastException && exc.Error.Message.Contains("Newtonsoft.Json")) + { + ctx.Response.StatusCode = 400; + await ctx.Response.WriteAsync("{\"message\":\"400: Bad Request\",\"code\":0}"); + return; + } + if (exc.Error is not PKError) { ctx.Response.StatusCode = 500;