diff --git a/src/api/users.py b/src/api/users.py index d26caa0..93a5fe9 100644 --- a/src/api/users.py +++ b/src/api/users.py @@ -4,6 +4,7 @@ from typing import Annotated, final from beanie import PydanticObjectId from fastapi import APIRouter, Body, HTTPException, Response, status from pydantic import BaseModel, EmailStr, StringConstraints +from pymongo.errors import DuplicateKeyError from starlette.status import HTTP_204_NO_CONTENT from src.api.auth.dependencies import LoggedInDep @@ -184,7 +185,13 @@ async def patch_user( detail="You're not authorized to update this user", ) - user = await data.update_user(user) + try: + user = await data.update_user(user) + except DuplicateKeyError: + raise HTTPException( + status_code=status.HTTP_409_CONFLICT, + detail="Username or email already taken", + ) return UserData.from_user(user)