Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom exception handler #60

Closed
onosendi opened this issue Jan 4, 2021 · 3 comments
Closed

Custom exception handler #60

onosendi opened this issue Jan 4, 2021 · 3 comments

Comments

@onosendi
Copy link

onosendi commented Jan 4, 2021

Any plans to add custom exception handlers for authentication and return a custom response? Here's a reference: https://indominusbyte.github.io/fastapi-jwt-auth/usage/optional/

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

vitalik commented Feb 13, 2021

Hi @onosendi

well fastapi here uses the starlette's exception handler mechanism

as for django-ninja - you can use django middleware layer

def error_handler_middleware(get_response):

    def middleware(request):
        try:
             return get_response(request)
        except AuthJWTException as e:
             return JSONResponse(...)

    return middleware

@vitalik
Copy link
Owner

vitalik commented Mar 3, 2021

@onosendi
this is now implemented in 0.11.0

@onosendi
Copy link
Author

onosendi commented Mar 3, 2021

@vitalik
Sweet man, thanks for the heads up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants