foxnouns/foxnouns/exceptions.py
2024-03-13 17:03:18 +01:00

76 lines
2.4 KiB
Python

import enum
class ErrorCode(enum.IntEnum):
BadRequest = 400
Forbidden = 403
NotFound = 404
MethodNotAllowed = 405
TooManyRequests = 429
InternalServerError = 500 # catch-all code for unknown errors
# Login/authorize error codes
InvalidState = 1001
InvalidOAuthCode = 1002
InvalidToken = 1003 # a token was supplied, but it is invalid
InviteRequired = 1004
InvalidTicket = 1005 # invalid signup ticket
InvalidUsername = 1006 # invalid username (when signing up)
UsernameTaken = 1007 # username taken (when signing up)
InvitesDisabled = 1008 # invites are disabled (unneeded)
InviteLimitReached = 1009 # invite limit reached (when creating invites)
InviteAlreadyUsed = 1010 # invite already used (when signing up)
DeletionPending = 1011 # own user deletion pending, returned with undo code
RecentExport = 1012 # latest export is too recent
UnsupportedInstance = 1013 # unsupported fediverse software
AlreadyLinked = 1014 # user already has linked account of the same type
NotLinked = 1015 # user already doesn't have a linked account
LastProvider = (
1016 # unlinking provider would leave account with no authentication method
)
InvalidCaptcha = 1017 # invalid or missing captcha response
# User-related error codes
UserNotFound = 2001
MemberListPrivate = 2002
FlagLimitReached = 2003
RerollingTooQuickly = 2004
# Member-related error codes
MemberNotFound = 3001
MemberLimitReached = 3002
MemberNameInUse = 3003
NotOwnMember = 3004
# General request error codes
RequestTooBig = 4001
MissingPermissions = 4002
# Moderation related error codes
ReportAlreadyHandled = 5001
NotSelfDelete = 5002
class ExpectedError(Exception):
msg: str
type: ErrorCode
status_code: int = 500
def __init__(self, msg: str, type: ErrorCode):
self.msg = msg
self.type = type
super().__init__(msg)
def __str__(self):
return f"{self.__class__.__name__}({self.msg})"
class NotFoundError(ExpectedError):
status_code = 404
def __init__(self, msg: str, type=ErrorCode.NotFound):
self.type = type
super().__init__(msg, type)
class ForbiddenError(ExpectedError):
status_code = 403
def __init__(self, msg: str, type=ErrorCode.Forbidden):
self.type = type
super().__init__(msg, type)