supertokens / supertokens-python

Python SDK for SuperTokens
https://supertokens.com
Other
119 stars 31 forks source link

python sdk delete_user throwing general error with no method #504

Open EricChristensen opened 1 month ago

EricChristensen commented 1 month ago

We are using the python sdk at version 0.17.0 using python version 3.11 from the public.ecr.aws/lambda/python:3.11 and running the backend SDK via a Lambda and API Gateway integration using the magnum and uvicorn.

The error logs that we are receiving when trying to call this method is:

2024-05-15T17:19:20.330Z    [ERROR] 2024-05-15T17:19:20.323Z f98ca373-f523-4419-99e3-bd59e888b49d An error occurred running the application.
2024-05-15T17:19:20.330Z    Traceback (most recent call last):
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/mangum/protocols/http.py", line 58, in run
2024-05-15T17:19:20.330Z    await app(self.scope, self.receive, self.send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/cors.py", line 83, in __call__
2024-05-15T17:19:20.330Z    await self.app(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/fastapi/applications.py", line 1106, in __call__
2024-05-15T17:19:20.330Z    await super().__call__(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
2024-05-15T17:19:20.330Z    await self.middleware_stack(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
2024-05-15T17:19:20.330Z    raise exc
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
2024-05-15T17:19:20.330Z    await self.app(scope, receive, _send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 108, in __call__
2024-05-15T17:19:20.330Z    response = await self.dispatch_func(request, call_next)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/task/member/member.py", line 283, in dispatch
2024-05-15T17:19:20.330Z    response = await call_next(request)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 84, in call_next
2024-05-15T17:19:20.330Z    raise app_exc
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
2024-05-15T17:19:20.330Z    await self.app(scope, receive_or_disconnect, send_no_error)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 108, in __call__
2024-05-15T17:19:20.330Z    response = await self.dispatch_func(request, call_next)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/framework/fastapi/fastapi_middleware.py", line 66, in dispatch
2024-05-15T17:19:20.330Z    result: Union[BaseResponse, None] = await st.handle_supertokens_error(
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/supertokens.py", line 666, in handle_supertokens_error
2024-05-15T17:19:20.330Z    raise err
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/framework/fastapi/fastapi_middleware.py", line 53, in dispatch
2024-05-15T17:19:20.330Z    response = await call_next(request)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 84, in call_next
2024-05-15T17:19:20.330Z    raise app_exc
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/base.py", line 70, in coro
2024-05-15T17:19:20.330Z    await self.app(scope, receive_or_disconnect, send_no_error)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
2024-05-15T17:19:20.330Z    raise exc
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
2024-05-15T17:19:20.330Z    await self.app(scope, receive, sender)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
2024-05-15T17:19:20.330Z    raise e
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
2024-05-15T17:19:20.330Z    await self.app(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
2024-05-15T17:19:20.330Z    await route.handle(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
2024-05-15T17:19:20.330Z    await self.app(scope, receive, send)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
2024-05-15T17:19:20.330Z    response = await func(request)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/fastapi/routing.py", line 274, in app
2024-05-15T17:19:20.330Z    raw_response = await run_endpoint_function(
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/fastapi/routing.py", line 191, in run_endpoint_function
2024-05-15T17:19:20.330Z    return await dependant.call(**values)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/task/member/member.py", line 109, in delete_user_
2024-05-15T17:19:20.330Z    await delete_user(user_id.user_uid)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/asyncio/__init__.py", line 81, in delete_user
2024-05-15T17:19:20.330Z    return await Supertokens.get_instance().delete_user(user_id, user_context)
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/supertokens.py", line 359, in delete_user
2024-05-15T17:19:20.330Z    await querier.send_post_request(
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/querier.py", line 275, in send_post_request
2024-05-15T17:19:20.330Z    return await self.__send_request_helper(path, "POST", f, len(self.__hosts))
2024-05-15T17:19:20.330Z    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/querier.py", line 436, in __send_request_helper
2024-05-15T17:19:20.330Z    raise_general_exception(e)
2024-05-15T17:19:20.330Z    File "/var/lang/lib/python3.11/site-packages/supertokens_python/exceptions.py", line 25, in raise_general_exception
2024-05-15T17:19:20.330Z    raise GeneralError(msg) from None
2024-05-15T17:19:20.330Z    supertokens_python.exceptions.GeneralError

An issue in the querier usually indicates that there was an issue from the core service, but there are no error logs from our core service for this call.

As requested by RP in discord our supertokens init is as follows:

supertokens_config = SupertokensConfig(
    connection_uri=os.getenv('SUPER_TOKENS_CORE_SERVICE_URL'),
    api_key=os.getenv('SUPER_TOKENS_API_KEY')
)

app_info = InputAppInfo(
    app_name="Our app name",
    api_domain=os.getenv('API_GATEWAY_URL'),
    website_domain="http://localhost:3000",
    api_base_path="/v1/member/auth",
    website_base_path="/auth",
    api_gateway_path="", 
)

framework = "fastapi"

recipe_list = [
    get_session_recipe(),
    get_email_password_recipe(),
    userroles.init(),
    get_passwordless_recipe(),
    usermetadata.init()
]

init(
    supertokens_config=config.supertokens_config, 
    app_info=config.app_info, 
    framework=config.framework, 
    recipe_list=config.recipe_list, 
    mode="asgi",
)

I also tried using the latest version of the sdk and it still produced the same error.

sattvikc commented 1 month ago

Hi @EricChristensen,

I was unable to reproduce this issue, maybe I am missing something. I created a fastAPI app and then added an endpoint from which I called the delete_user. And things seem to work fine.

@app.get("/delete")
async def delete_user_api():
    await delete_user('<some user id>')
    return {
        "status": "OK",
    }

Would it be possible to provide a minimal setup that reproduces this issue? Alternatively, I would also like you to add try/except around your delete call and help me with the type and msg of the exception.

EricChristensen commented 1 month ago

I added the try catch around the delete user and printed the exception which was completely empty. Before adding the explicit

return {
        "status": "OK",
    }

The response we were getting was

{
    "status": "GENERAL_ERROR",
    "message": ""
}

Which makes sense based on the last two log lines from the error output

2024-05-15T17:19:20.330Z    raise GeneralError(msg) from None
2024-05-15T17:19:20.330Z    supertokens_python.exceptions.GeneralError

It looks like the even though this error occurs, the user does get successfully deleted, it just takes a little bit of time to take affect.

rishabhpoddar commented 1 month ago

Where are you calling this function? In your custom application API, or in one of our overrides?

I added the try catch around the delete user and printed the exception which was completely empty. Before adding the explicit

What do you mean by before adding the explicit (return ok response)?

The response we were getting was

This is the response from the API? Our sdk does not translate GeneralException to this type of response from our APIs. GeneralExcexptions are just like python exceptions that get propagated to your app's error handler and however you deal with them, is up to you.

Maybe you can make a demo app showcasing this issue? Or post your api code here so we get clarity?

EricChristensen commented 1 month ago

Will follow up on this by the end of the week and address your questions. We are switching over to passwordless only from email this week so I will be circling back to the delete function as part of that.