feat(apiv2): ignore exception caused by invalid user-provided JSON

return 400 bad request instead
This commit is contained in:
spiral 2021-10-12 08:34:28 -04:00
parent a20276f6e6
commit eb05cbf76c
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31

View File

@ -133,6 +133,15 @@ namespace PluralKit.API
app.UseExceptionHandler(handler => handler.Run(async ctx =>
{
var exc = ctx.Features.Get<IExceptionHandlerPathFeature>();
// 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;