mirror of
https://github.com/xlorepdarkhelm/numinar-coding-project.git
synced 2026-09-05 22:18:26 -04:00
Better error handling for endpoint.
Signed-off-by: Cliff Hill <xlorep@darkhelm.org>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user