I'm using mongodb-atlas(cloud version) and fastAPI for learning to create backend API.
I created this router post that will create users. I tried to raise an exception when trying to create an email that already exist in mongodb-atlas. The code goes like this
@user_router.post('/create', summary="Create new user")async def create_user(data: UserAuth): try: return await UserService.create_user(data) except pymongo.errors.DuplicateKeyError: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="User with this email or username already exist" )
But the problem is when manually testing this and trying out the API, instead of getting the error message "User with this email or username already exist" I simply just get 500 Internal Server error.
The terminal mention that:
during the handling of the above exception, another exception occurred:
Traceback (most recent call last): File "\\lib\\site-packages\\uvicorn\\protocols\\http\\httptools_impl.py", line 426, in run_asgi result = await app( # type: ignore\[func-returns-value\] File "\\lib\\site-packages\\uvicorn\\middleware\\proxy_headers.py", line 84, in __call__ return await self.app(scope, receive, send) File "\\lib\\site-packages\\fastapi\\applications.py", line 1106, in __call__ await super().__call__(scope, receive, send) File "\\lib\\site-packages\\starlette\\applications.py", line 122, in __call__ await self.middleware_stack(scope, receive, send) File "\\lib\\site-packages\\starlette\\middleware\\errors.py", line 184, in __call__ raise exc File "\\lib\\site-packages\\starlette\\middleware\\errors.py", line 162, in __call__ await self.app(scope, receive, \_send) File "\\lib\\site-packages\\starlette\\middleware\\exceptions.py", line 79, in __call__ raise exc File "\\lib\\site-packages\\starlette\\middleware\\exceptions.py", line 68, in __call__ await self.app(scope, receive, sender) File "\\lib\\site-packages\\fastapi\\middleware\\asyncexitstack.py", line 20, in __call__ raise e File "\\lib\\site-packages\\fastapi\\middleware\\asyncexitstack.py", line 17, in __call__ await self.app(scope, receive, send) File "\\lib\\site-packages\\starlette\\routing.py", line 718, in __call__ await route.handle(scope, receive, send) File "\\lib\\site-packages\\starlette\\routing.py", line 276, in handle await self.app(scope, receive, send) File "\\lib\\site-packages\\starlette\\routing.py", line 66, in app response = await func(request) File "\\lib\\site-packages\\fastapi\\routing.py", line 274, in app raw_response = await run_endpoint_function( File "\\lib\\site-packages\\fastapi\\routing.py", line 191, in run_endpoint_function return await dependant.call(\*\*values) File "\\Backend\\App\\api\\api_v1\\handlers\\user.py", line 13, in create_user return await UserService.create_user(data) File "\\Backend\\App\\services\\user_service.py", line 15, in create_user await user_in.save() File "\\lib\\site-packages\\beanie\\odm\\actions.py", line 219, in wrapper result = await f(self, \*args, skip_actions=skip_actions, \*\*kwargs) File "\\lib\\site-packages\\beanie\\odm\\utils\\state.py", line 66, in wrapper result = await f(self, \*args, \*\*kwargs) File "\\lib\\site-packages\\beanie\\odm\\utils\\self_validation.py", line 12, in wrapper return await f(self, \*args, \*\*kwargs) File "\\lib\\site-packages\\beanie\\odm\\documents.py", line 545, in save return await self.update( File "\\lib\\site-packages\\beanie\\odm\\actions.py", line 219, in wrapper result = await f(self, \*args, skip_actions=skip_actions, \*\*kwargs) File "\\lib\\site-packages\\beanie\\odm\\utils\\state.py", line 66, in wrapper result = await f(self, \*args, \*\*kwargs) File "\\lib\\site-packages\\beanie\\odm\\documents.py", line 670, in update raise RevisionIdWasChanged beanie.exceptions.RevisionIdWasChanged
I have zero idea what this is, I tried to fix it but since I have little understanding about beanie I couldn't comprehend how this is happening.Please let me know what I can do to resolve this.I also understand that I only show a snippet of my code if there needs to be added to help understand the issue please let me know, I'll try to give as much more detail as I can.
I haven't tried that much since I got little to no understanding on how and why this occur. My expectation is that this error message shows "User with this email or username already exist"
instead of error 500 Internal Server Error in API docs