2021-10-12 07:01:02 +00:00
using System ;
2021-10-13 12:37:34 +00:00
using System.Collections.Generic ;
2021-10-12 07:01:02 +00:00
using Newtonsoft.Json.Linq ;
2021-10-13 12:37:34 +00:00
using PluralKit.Core ;
2021-10-12 07:01:02 +00:00
namespace PluralKit.API
{
public class PKError : Exception
{
public int ResponseCode { get ; init ; }
public int JsonCode { get ; init ; }
public PKError ( int code , int json_code , string message ) : base ( message )
{
ResponseCode = code ;
JsonCode = json_code ;
}
public JObject ToJson ( )
{
var j = new JObject ( ) ;
j . Add ( "message" , this . Message ) ;
j . Add ( "code" , this . JsonCode ) ;
return j ;
}
}
public class ModelParseError : PKError
{
2021-10-13 12:37:34 +00:00
private IEnumerable < ValidationError > _errors { get ; init ; }
public ModelParseError ( IEnumerable < ValidationError > errors ) : base ( 400 , 40001 , "Error parsing JSON model" )
2021-10-12 07:01:02 +00:00
{
2021-10-13 12:37:34 +00:00
_errors = errors ;
2021-10-12 07:01:02 +00:00
}
public new JObject ToJson ( )
{
var j = base . ToJson ( ) ;
2021-10-13 12:37:34 +00:00
var e = new JObject ( ) ;
foreach ( var err in _errors )
{
var o = new JObject ( ) ;
if ( err is FieldTooLongError fe )
{
o . Add ( "message" , $"Field {err.Key} is too long." ) ;
o . Add ( "actual_length" , fe . ActualLength ) ;
o . Add ( "max_length" , fe . MaxLength ) ;
}
else if ( err . Text ! = null )
o . Add ( "message" , err . Text ) ;
else
o . Add ( "message" , $"Field {err.Key} is invalid." ) ;
2021-10-30 00:06:09 +00:00
if ( e [ err . Key ] = = null )
e . Add ( err . Key , new JArray ( ) ) ;
2021-10-13 12:37:34 +00:00
2021-10-30 00:06:09 +00:00
( e [ err . Key ] as JArray ) . Add ( o ) ;
2021-10-13 12:37:34 +00:00
}
2021-10-12 07:01:02 +00:00
2021-10-13 12:37:34 +00:00
j . Add ( "errors" , e ) ;
2021-10-12 07:01:02 +00:00
return j ;
}
}
2021-10-13 13:08:17 +00:00
public static class Errors
2021-10-12 07:01:02 +00:00
{
public static PKError GenericBadRequest = new ( 400 , 0 , "400: Bad Request" ) ;
2021-10-12 09:17:54 +00:00
public static PKError GenericAuthError = new ( 401 , 0 , "401: Missing or invalid Authorization header" ) ;
2021-10-12 07:01:02 +00:00
public static PKError SystemNotFound = new ( 404 , 20001 , "System not found." ) ;
public static PKError MemberNotFound = new ( 404 , 20002 , "Member not found." ) ;
2021-10-30 22:16:18 +00:00
public static PKError MemberNotFoundWithRef ( string memberRef ) = > new ( 404 , 20003 , $"Member '{memberRef}' not found." ) ;
public static PKError GroupNotFound = new ( 404 , 20004 , "Group not found." ) ;
public static PKError GroupNotFoundWithRef ( string groupRef ) = > new ( 404 , 20005 , $"Group '{groupRef}' not found." ) ;
public static PKError MessageNotFound = new ( 404 , 20006 , "Message not found." ) ;
public static PKError SwitchNotFound = new ( 404 , 20007 , "Switch not found." ) ;
public static PKError SwitchNotFoundPublic = new ( 404 , 20008 , "Switch not found, switch associated with different system, or unauthorized to view front history." ) ;
public static PKError SystemGuildNotFound = new ( 404 , 20009 , "No system guild settings found for target guild." ) ;
public static PKError MemberGuildNotFound = new ( 404 , 20010 , "No member guild settings found for target guild." ) ;
2021-10-12 07:01:02 +00:00
public static PKError UnauthorizedMemberList = new ( 403 , 30001 , "Unauthorized to view member list" ) ;
public static PKError UnauthorizedGroupList = new ( 403 , 30002 , "Unauthorized to view group list" ) ;
2021-10-12 09:17:54 +00:00
public static PKError UnauthorizedGroupMemberList = new ( 403 , 30003 , "Unauthorized to view group member list" ) ;
public static PKError UnauthorizedCurrentFronters = new ( 403 , 30004 , "Unauthorized to view current fronters." ) ;
2021-10-12 12:33:31 +00:00
public static PKError UnauthorizedFrontHistory = new ( 403 , 30005 , "Unauthorized to view front history." ) ;
2021-10-13 05:02:34 +00:00
public static PKError NotOwnMemberError = new ( 403 , 30006 , "Target member is not part of your system." ) ;
2021-10-13 09:29:33 +00:00
public static PKError NotOwnGroupError = new ( 403 , 30007 , "Target group is not part of your system." ) ;
2021-10-12 12:33:31 +00:00
// todo: somehow add the memberRef to the JSON
2021-10-13 05:02:34 +00:00
public static PKError NotOwnMemberErrorWithRef ( string memberRef ) = > new ( 403 , 30008 , $"Member '{memberRef}' is not part of your system." ) ;
public static PKError NotOwnGroupErrorWithRef ( string groupRef ) = > new ( 403 , 30009 , $"Group '{groupRef}' is not part of your system." ) ;
public static PKError MissingAutoproxyMember = new ( 400 , 40002 , "Missing autoproxy member for member-mode autoproxy." ) ;
2021-10-13 09:29:33 +00:00
public static PKError DuplicateMembersInList = new ( 400 , 40003 , "Duplicate members in member list." ) ;
public static PKError SameSwitchMembersError = new ( 400 , 40004 , "Member list identical to current fronter list." ) ;
public static PKError SameSwitchTimestampError = new ( 400 , 40005 , "Switch with provided timestamp already exists." ) ;
public static PKError InvalidSwitchId = new ( 400 , 40006 , "Invalid switch ID." ) ;
2021-11-03 06:42:37 +00:00
public static PKError MemberLimitReached = new ( 400 , 40007 , "Member limit reached." ) ;
public static PKError GroupLimitReached = new ( 400 , 40008 , "Group limit reached." ) ;
2021-10-12 07:01:02 +00:00
public static PKError Unimplemented = new ( 501 , 50001 , "Unimplemented" ) ;
}
2021-10-13 13:08:17 +00:00
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 ;
}
}
2021-10-12 07:01:02 +00:00
}