Fix async generator exception handling in database dependencies

This commit is contained in:
2026-01-23 16:25:52 +01:00
parent faac14346f
commit 9fb93794e6
2 changed files with 102 additions and 16 deletions

View File

@@ -124,19 +124,15 @@ async def get_database_session() -> AsyncGenerator:
"""
try:
from src.server.database import get_db_session
async with get_db_session() as session:
try:
yield session
except Exception:
# Re-raise the exception to let FastAPI handle it
# This prevents "generator didn't stop after athrow()" error
raise
except ImportError:
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Database functionality not installed"
)
try:
async with get_db_session() as session:
yield session
except RuntimeError as e:
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
@@ -168,17 +164,17 @@ async def get_optional_database_session() -> AsyncGenerator:
"""
try:
from src.server.database import get_db_session
async with get_db_session() as session:
try:
yield session
except Exception:
# Re-raise to let FastAPI handle it properly
# This prevents "generator didn't stop after athrow()" error
raise
except (ImportError, RuntimeError):
# Database not available - yield None
yield None
return
try:
async with get_db_session() as session:
yield session
except (ImportError, RuntimeError):
# Database became unavailable - this shouldn't happen but handle it
yield None
def get_current_user(