fix(api): don't 500 on invalid PATCH body

This commit is contained in:
spiral 2022-03-23 14:26:54 -04:00
parent a20f0916ac
commit 97c14b20b0
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
3 changed files with 21 additions and 5 deletions

View File

@ -127,6 +127,10 @@ public static class APIErrorHandlerExt
if (exc is FormatException && exc.Message.Contains("was not recognized as a valid DateTime"))
return true;
// this happens if a user sends an empty JSON object for PATCH (or a JSON object with no valid keys)
if (exc is InvalidPatchException)
return true;
// This may expanded at some point.
return false;
}

View File

@ -14,11 +14,23 @@ internal class QueryPatchWrapper
return this;
}
public Query ToQuery(Query q) => q.AsUpdate(_dict);
public Query ToQuery(Query q)
{
try
{
return q.AsUpdate(_dict);
}
catch (InvalidOperationException)
{
throw new InvalidPatchException();
}
}
}
internal static class SqlKataExtensions
{
internal static Query ApplyPatch(this Query query, Func<QueryPatchWrapper, QueryPatchWrapper> func)
=> func(new QueryPatchWrapper()).ToQuery(query);
}
}
public class InvalidPatchException : Exception {}

View File

@ -7,7 +7,8 @@ permalink: /api/models
A question mark (`?`) next to the *key name* means the key is optional - it may be omitted in API responses. A question mark next to the *key type* means the key is nullable - API responses may return `null` for that key, instead of the specified type.
In PATCH endpoints, all keys are optional. However, providing an object with no keys (or no valid keys) will result in 500 internal server error.
In PATCH endpoints, all keys are optional. However, you must provide at least one valid key to update; please use a GET request if you want to query the existing information.
<br>Sending a PATCH request with an empty JSON object, or with a JSON object that contains no valid keys for the target entity, will result in a 400 bad request error.
Privacy objects (`privacy` key in models) contain values "private" or "public". Patching a privacy value to `null` will set to public. If you do not have access to view the privacy object of the member, the value of the `privacy` key will be null, rather than the values of individual privacy keys.
@ -93,8 +94,7 @@ Every PluralKit entity has two IDs: a short (5-character) ID and a longer UUID.
|---|---|---|
|timestamp|datetime||
|id|snowflake|The ID of the message sent by the webhook. Encoded as string for precision reasons.|
|original| snowflake|The ID of the (now-deleted) message that triggered the proxy. Encoded as string for precision reasons.|
|sender|snowflake|The user ID of the account that triggered the proxy. Encoded as string for precision reasons.|
|original| snowflake|The ID of the (now-deleted) message that triggered the proxy. Encoded as string for precision reasons.||sender|snowflake|The user ID of the account that triggered the proxy. Encoded as string for precision reasons.|
|channel|snowflake|The ID of the channel the message was sent in. Encoded as string for precision reasons.|
|guild|snowflake|The ID of the server the message was sent in. Encoded as string for precision reasons.|
|system?|full System object|The system that proxied the message. Null if the member associated with this message was deleted.|