Skip to content

ErrorHandler

authx._internal._error._ErrorHandler

_ErrorHandler()

Base Handler for FastAPI handling AuthX exceptions

Base Handler for FastAPI handling AuthX exceptions

Source code in authx/_internal/_error.py
def __init__(self) -> None:
    """Base Handler for FastAPI handling AuthX exceptions"""

    self.MSG_DEFAULT = "AuthX Error"
    self.MSG_TOKEN_ERROR = "Token Error"
    self.MSG_MISSING_TOKEN_ERROR = "Missing JWT in request"
    self.MSG_MISSING_CSRF_ERROR = "Missing CSRF double submit token in request"
    self.MSG_TOKEN_TYPE_ERROR = "Bad token type"
    self.MSG_REVOKED_TOKEN_ERROR = "Invalid token"
    self.MSG_TOKEN_REQUIRED_ERROR = "Token required"
    self.MSG_FRESH_TOKEN_REQUIRED_ERROR = "Fresh token required"
    self.MSG_ACCESS_TOKEN_REQUIRED_ERROR = "Access token required"
    self.MSG_REFRESH_TOKEN_REQUIRED_ERROR = "Refresh token required"
    self.MSG_CSRF_ERROR = "CSRF double submit does not match"
    self.MSG_DECODE_JWT_ERROR = "Invalid Token"

MSG_DEFAULT instance-attribute

MSG_DEFAULT = 'AuthX Error'

MSG_TOKEN_ERROR instance-attribute

MSG_TOKEN_ERROR = 'Token Error'

MSG_MISSING_TOKEN_ERROR instance-attribute

MSG_MISSING_TOKEN_ERROR = 'Missing JWT in request'

MSG_MISSING_CSRF_ERROR instance-attribute

MSG_MISSING_CSRF_ERROR = 'Missing CSRF double submit token in request'

MSG_TOKEN_TYPE_ERROR instance-attribute

MSG_TOKEN_TYPE_ERROR = 'Bad token type'

MSG_REVOKED_TOKEN_ERROR instance-attribute

MSG_REVOKED_TOKEN_ERROR = 'Invalid token'

MSG_TOKEN_REQUIRED_ERROR instance-attribute

MSG_TOKEN_REQUIRED_ERROR = 'Token required'

MSG_FRESH_TOKEN_REQUIRED_ERROR instance-attribute

MSG_FRESH_TOKEN_REQUIRED_ERROR = 'Fresh token required'

MSG_ACCESS_TOKEN_REQUIRED_ERROR instance-attribute

MSG_ACCESS_TOKEN_REQUIRED_ERROR = 'Access token required'

MSG_REFRESH_TOKEN_REQUIRED_ERROR instance-attribute

MSG_REFRESH_TOKEN_REQUIRED_ERROR = 'Refresh token required'

MSG_CSRF_ERROR instance-attribute

MSG_CSRF_ERROR = 'CSRF double submit does not match'

MSG_DECODE_JWT_ERROR instance-attribute

MSG_DECODE_JWT_ERROR = 'Invalid Token'

handle_errors

handle_errors(app)

Add the FastAPI.exception_handlers relative to AuthX exceptions

PARAMETER DESCRIPTION
app

TYPE: FastAPI

PARAMETER DESCRIPTION
app

the FastAPI application to handle errors for

TYPE: FastAPI

Source code in authx/_internal/_error.py
def handle_errors(self, app: FastAPI) -> None:
    """Add the `FastAPI.exception_handlers` relative to AuthX exceptions

    Args:
        app (FastAPI): the FastAPI application to handle errors for
    """
    self._set_app_exception_handler(
        app, exception=exceptions.JWTDecodeError, status_code=422, message=None
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.MissingTokenError,
        status_code=401,
        message=self.MSG_MISSING_TOKEN_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.MissingCSRFTokenError,
        status_code=401,
        message=self.MSG_MISSING_CSRF_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.TokenTypeError,
        status_code=401,
        message=self.MSG_TOKEN_TYPE_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.RevokedTokenError,
        status_code=401,
        message=self.MSG_REVOKED_TOKEN_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.TokenRequiredError,
        status_code=401,
        message=self.MSG_TOKEN_REQUIRED_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.FreshTokenRequiredError,
        status_code=401,
        message=self.MSG_FRESH_TOKEN_REQUIRED_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.AccessTokenRequiredError,
        status_code=401,
        message=self.MSG_ACCESS_TOKEN_REQUIRED_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.RefreshTokenRequiredError,
        status_code=401,
        message=self.MSG_REFRESH_TOKEN_REQUIRED_ERROR,
    )
    self._set_app_exception_handler(
        app,
        exception=exceptions.CSRFError,
        status_code=401,
        message=self.MSG_CSRF_ERROR,
    )