Skip to content

Commit

Permalink
Handle HTTPException from dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mongkok committed Feb 13, 2024
1 parent 97ee527 commit 6d89eb8
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions debug_toolbar/panels/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from contextlib import AsyncExitStack
from time import perf_counter

from fastapi import Request, Response
from fastapi import HTTPException, Request, Response
from fastapi.dependencies.utils import solve_dependencies
from sqlalchemy import event
from sqlalchemy.engine import Connection, Engine, ExecutionContext
Expand Down Expand Up @@ -44,20 +44,24 @@ async def add_engines(self, request: Request):
route = request["route"]

if hasattr(route, "dependant"):
solved_result = await solve_dependencies(
request=request,
dependant=route.dependant,
dependency_overrides_provider=route.dependency_overrides_provider,
async_exit_stack=AsyncExitStack(),
)
for value in solved_result[0].values():
if isinstance(value, Session):
bind = value.get_bind()
try:
solved_result = await solve_dependencies(
request=request,
dependant=route.dependant,
dependency_overrides_provider=route.dependency_overrides_provider,
async_exit_stack=AsyncExitStack(),
)
except HTTPException:
pass
else:
for value in solved_result[0].values():
if isinstance(value, Session):
bind = value.get_bind()

if isinstance(bind, Connection):
self.engines.add(bind.engine)
else:
self.engines.add(bind)
if isinstance(bind, Connection):
self.engines.add(bind.engine)
else:
self.engines.add(bind)

async def process_request(self, request: Request) -> Response:
await self.add_engines(request)
Expand Down

0 comments on commit 6d89eb8

Please sign in to comment.