IndominusByte / fastapi-jwt-auth

FastAPI extension that provides JWT Auth support (secure, easy to use, and lightweight)
http://indominusbyte.github.io/fastapi-jwt-auth/
MIT License
627 stars 143 forks source link

Error hierarchy #81

Open Midnighter opened 2 years ago

Midnighter commented 2 years ago

Hi,

At the moment, there is the base error class

class AuthJWTException(Exception):
    """
    Base except which all fastapi_jwt_auth errors extend
    """
    pass

and then all the others inherit from that and add the two attributes status_code and message. In the docs, you suggest the following handler:

@app.exception_handler(AuthJWTException)
def authjwt_exception_handler(request: Request, exc: AuthJWTException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.message}
    )

which uses the error's attributes. Technically, the annotated type does not have these attributes.

I don't know why you chose this design but I would like to make a PR to change it to:

class AuthJWTException(Exception):
    """
    Base exception which all fastapi_jwt_auth errors extend.
    """

    def __init__(self,status_code: int, message: str, **kwargs):
        super().__init__(**kwargs)
        self.status_code = status_code
        self.message = message

and then change all others to the following init:

    def __init__(self,status_code: int, message: str, **kwargs):
        super().__init__(status_code=status_code, message=message, **kwargs)
SergeyKapshuchenko commented 1 year ago

same problem, any updates on this ?