67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
from fastapi import HTTPException, status
|
|
|
|
|
|
class BasicException(HTTPException):
|
|
base_status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
base_detail: str = 'Something went wrong'
|
|
|
|
def __init__(
|
|
self, *, status_code: int | None = None, detail: str | None = None
|
|
):
|
|
status_code = status_code or self.base_status_code
|
|
detail = detail or self.base_detail
|
|
super().__init__(status_code=status_code, detail=detail)
|
|
|
|
@classmethod
|
|
def description(cls, detail: str | None = None):
|
|
return {
|
|
'description': detail or cls.base_detail,
|
|
'content': {
|
|
'application/json': {
|
|
'example': {
|
|
'detail': detail or cls.base_detail,
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
class BadRequestException(BasicException):
|
|
base_status_code: int = status.HTTP_400_BAD_REQUEST
|
|
base_detail: str = 'Bad Request'
|
|
|
|
|
|
class UnauthorizedException(BasicException):
|
|
base_status_code: int = status.HTTP_401_UNAUTHORIZED
|
|
base_detail: str = 'Unauthorized'
|
|
|
|
|
|
class ForbiddenException(BasicException):
|
|
base_status_code: int = status.HTTP_403_FORBIDDEN
|
|
base_detail: str = 'Forbidden'
|
|
|
|
|
|
class NotFoundException(BasicException):
|
|
base_status_code: int = status.HTTP_404_NOT_FOUND
|
|
base_detail: str = 'Not Found'
|
|
|
|
|
|
class ConflictException(BasicException):
|
|
base_status_code: int = status.HTTP_409_CONFLICT
|
|
base_detail: str = 'Conflict'
|
|
|
|
|
|
class TooManyRequestsException(BasicException):
|
|
base_status_code: int = status.HTTP_429_TOO_MANY_REQUESTS
|
|
base_detail: str = 'Too Many Requests'
|
|
|
|
|
|
class InternalServerErrorException(BasicException):
|
|
base_status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
base_detail: str = 'Internal Server Error'
|
|
|
|
|
|
class UnknownException(BasicException):
|
|
base_status_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
base_detail: str = 'Unknown error'
|