Skip to content

Commit 1ddb2cc

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 bdadcda commit 1ddb2cc

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
@@ -17,6 +17,7 @@
1717
SecurityScopes
1818
)
1919
from bson import ObjectId, errors
20+
from pymongo.errors import DuplicateKeyError
2021
from .auth import Authentication, Token
2122
from .db import Database
2223
from .models import Node, User, Password
@@ -94,6 +95,11 @@ async def post_user(
9495
status_code=status.HTTP_400_BAD_REQUEST,
9596
detail=str(error)
9697
) from error
98+
except DuplicateKeyError as error:
99+
raise HTTPException(
100+
status_code=status.HTTP_400_BAD_REQUEST,
101+
detail=f"{username} is already taken.Try with different username."
102+
) from error
97103
await pubsub.publish_cloudevent('user', {'op': operation,
98104
'id': str(obj.id)})
99105
return obj

0 commit comments

Comments
 (0)