Skip to content

Commit

Permalink
Extract constructor to parent class (#30)
Browse files Browse the repository at this point in the history
* Extract constructor to parent class

When handling errors as demonstrated [here](https://glitchcorp.github.io/fastapi-another-jwt-auth/usage/basic/), the `exc` object has no known members of `status_code` or `message`.

This change provides those members to `AuthJWTException` and all of its subclasses and prevents duplication.

* Format
  • Loading branch information
willsawyerrrr authored Jun 10, 2024
1 parent aa01bc2 commit 191241d
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions fastapi_another_jwt_auth/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,57 @@ class AuthJWTException(Exception):
"""
Base except which all fastapi_another_jwt_auth errors extend
"""
pass
def __init__(self, status_code: int, message: str):
self.status_code = status_code
self.message = message

class InvalidHeaderError(AuthJWTException):
"""
An error getting jwt in header or jwt header information from a request
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class JWTDecodeError(AuthJWTException):
"""
An error decoding a JWT
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class CSRFError(AuthJWTException):
"""
An error with CSRF protection
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class MissingTokenError(AuthJWTException):
"""
Error raised when token not found
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class RevokedTokenError(AuthJWTException):
"""
Error raised when a revoked token attempt to access a protected endpoint
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class AccessTokenRequired(AuthJWTException):
"""
Error raised when a valid, non-access JWT attempt to access an endpoint
protected by jwt_required, jwt_optional, fresh_jwt_required
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class RefreshTokenRequired(AuthJWTException):
"""
Error raised when a valid, non-refresh JWT attempt to access an endpoint
protected by jwt_refresh_token_required
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

class FreshTokenRequired(AuthJWTException):
"""
Error raised when a valid, non-fresh JWT attempt to access an endpoint
protected by fresh_jwt_required
"""
def __init__(self,status_code: int, message: str):
self.status_code = status_code
self.message = message
pass

0 comments on commit 191241d

Please sign in to comment.