Better error handling for endpoint.

Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
2025-08-23 22:33:42 -04:00
parent 986df52c94
commit 073f7df908
2 changed files with 5 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
from fastapi import APIRouter
from fastapi import HTTPException
from pydantic import EmailStr
from sqlalchemy.exc import NoResultFound
from backend.dependencies.db import DBSession
@@ -21,7 +22,7 @@ async def read_users(session: DBSession):
@router.get("/{email}", response_model=UserResponse)
async def read_user(email: str, session: DBSession):
async def read_user(email: EmailStr, session: DBSession):
"""Endpoint to retrieve a user by email."""
try:
user = await get_user(session, email)

View File

@@ -41,11 +41,10 @@ async def get_user(session: AsyncSession, email: str) -> User:
try:
stmt = select(User).where(User.email == email)
user = await session.scalar(stmt)
if user is None:
raise NoResultFound(f"User with email {email} not found")
logger.info(f"Successfully retrieved user with email: {email}")
return cast(User, user)
except NoResultFound:
logger.error(f"User with email {email} not found")
raise
return user
except Exception as e:
logger.error(f"Failed to retrieve user with email {email}: {str(e)}")
raise