refactor(apiv2): rename APIErrors to Errors, move IsUserError to helper method

This commit is contained in:
spiral
2021-10-13 09:08:17 -04:00
parent 431f7e8931
commit fd49e7e4ea
9 changed files with 98 additions and 84 deletions

View File

@@ -75,7 +75,7 @@ namespace PluralKit.API
}
}
public static class APIErrors
public static class Errors
{
public static PKError GenericBadRequest = new(400, 0, "400: Bad Request");
public static PKError GenericAuthError = new(401, 0, "401: Missing or invalid Authorization header");
@@ -104,4 +104,21 @@ namespace PluralKit.API
public static PKError InvalidSwitchId = new(400, 40006, "Invalid switch ID.");
public static PKError Unimplemented = new(501, 50001, "Unimplemented");
}
public static class APIErrorHandlerExt
{
public static bool IsUserError(this Exception exc)
{
// caused by users sending an incorrect JSON type (array where an object is expected, etc)
if (exc is InvalidCastException && exc.Message.Contains("Newtonsoft.Json"))
return true;
// Hacky parsing of timestamps results in hacky error handling. Probably fix this one at some point.
if (exc is FormatException && exc.Message.Contains("was not recognized as a valid DateTime"))
return true;
// This may expanded at some point.
return false;
}
}
}