Skip to content

Commit

Permalink
pysnippetGH-22: Add an exception_handler sample in the demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan committed Oct 12, 2023
1 parent 4993860 commit b946f89
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/demonstration/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from fastapi import APIRouter
from fastapi import FastAPI
from fastapi import Request
from fastapi.staticfiles import StaticFiles
from sqlalchemy.orm import Session
from starlette.responses import RedirectResponse

from config import oauth2_config
from database import Base
from database import engine
from database import get_db
from fastapi_oauth2.exceptions import OAuth2Error
from fastapi_oauth2.middleware import Auth
from fastapi_oauth2.middleware import OAuth2Middleware
from fastapi_oauth2.middleware import User
Expand Down Expand Up @@ -37,6 +40,15 @@ async def on_auth(auth: Auth, user: User):


app = FastAPI()


# https://fastapi.tiangolo.com/tutorial/handling-errors/
@app.exception_handler(OAuth2Error)
async def error_handler(request: Request, e: OAuth2Error):
print("An error occurred in OAuth2Middleware", e)
return RedirectResponse(url="/", status_code=303)


app.include_router(router_api)
app.include_router(router_ssr)
app.include_router(oauth2_router)
Expand Down

0 comments on commit b946f89

Please sign in to comment.