Skip to content

Commit 1d23e72

Browse files
author
Jeny Sadadia
committed
api.main: catch 'DuplicateKeyError' exception
Need to catch an exception and return proper error message when '/user' endpoint receives a username that has already been taken. Signed-off-by: Jeny Sadadia <[email protected]>
1 parent 0ecb0d5 commit 1d23e72

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

api/main.py

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
SecurityScopes
1616
)
1717
from bson import ObjectId, errors
18+
from pymongo.errors import DuplicateKeyError
1819
from .auth import Authentication, Token
1920
from .db import Database
2021
from .models import Node, Regression, User, Password, get_model_from_kind
@@ -92,6 +93,11 @@ async def post_user(
9293
status_code=status.HTTP_400_BAD_REQUEST,
9394
detail=str(error)
9495
) from error
96+
except DuplicateKeyError as error:
97+
raise HTTPException(
98+
status_code=status.HTTP_400_BAD_REQUEST,
99+
detail=f"{username} is already taken. Try with different username."
100+
) from error
95101
await pubsub.publish_cloudevent('user', {'op': operation,
96102
'id': str(obj.id)})
97103
return obj

0 commit comments

Comments
 (0)