Files
Wu Clan 61147d4636 Fix background task not executed (#86)
* Fix login auth failure exception

* Add background task to custom exception class

* Fix branch conflicts

* Update login log status to enum.
2023-05-31 16:16:10 +08:00

81 lines
2.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Any
from fastapi import HTTPException
from starlette.background import BackgroundTask
from backend.app.common.response.response_code import CustomCode
class BaseExceptionMixin(Exception):
code: int
def __init__(self, *, msg: str = None, data: Any = None, background: BackgroundTask | None = None):
self.msg = msg
self.data = data
# The original background task: https://www.starlette.io/background/
self.background = background
class HTTPError(HTTPException):
def __init__(self, *, code: int, msg: Any = None, headers: dict[str, Any] | None = None):
super().__init__(status_code=code, detail=msg, headers=headers)
class CustomError(BaseExceptionMixin):
def __init__(self, *, error: CustomCode, data: Any = None, background: BackgroundTask | None = None):
self.code = error.code
super().__init__(msg=error.msg, data=data, background=background)
class RequestError(BaseExceptionMixin):
code = 400
def __init__(self, *, msg: str = 'Bad Request', data: Any = None, background: BackgroundTask | None = None):
super().__init__(msg=msg, data=data, background=background)
class ForbiddenError(BaseExceptionMixin):
code = 403
def __init__(self, *, msg: str = 'Forbidden', data: Any = None, background: BackgroundTask | None = None):
super().__init__(msg=msg, data=data, background=background)
class NotFoundError(BaseExceptionMixin):
code = 404
def __init__(self, *, msg: str = 'Not Found', data: Any = None, background: BackgroundTask | None = None):
super().__init__(msg=msg, data=data, background=background)
class ServerError(BaseExceptionMixin):
code = 500
def __init__(
self, *, msg: str = 'Internal Server Error', data: Any = None, background: BackgroundTask | None = None
):
super().__init__(msg=msg, data=data, background=background)
class GatewayError(BaseExceptionMixin):
code = 502
def __init__(self, *, msg: str = 'Bad Gateway', data: Any = None, background: BackgroundTask | None = None):
super().__init__(msg=msg, data=data, background=background)
class AuthorizationError(BaseExceptionMixin):
code = 401
def __init__(self, *, msg: str = 'Permission denied', data: Any = None, background: BackgroundTask | None = None):
super().__init__(msg=msg, data=data, background=background)
class TokenError(HTTPError):
code = 401
def __init__(self, *, msg: str = 'Not authenticated', headers: dict[str, Any] | None = None):
super().__init__(code=self.code, msg=msg, headers=headers or {'WWW-Authenticate': 'Bearer'})