mongkok / fastapi-debug-toolbar

A debug toolbar for FastAPI.
https://fastapi-debug-toolbar.domake.io
BSD 3-Clause "New" or "Revised" License
132 stars 13 forks source link

Internal server error when using SQLAlchemyPanel #42

Closed anshidnm closed 6 months ago

anshidnm commented 7 months ago
app.add_middleware(
        DebugToolbarMiddleware,
        panels=["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"],
    )

when i add this middleware i got an ISE like fastapi.exceptions.HTTPException This exception is coming from current user Dependency.

when comment the panels parameter, its working. no ISE occuring. But need to track the sqlquery count. so can't comment it

app.add_middleware(
        DebugToolbarMiddleware,
       # panels=["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"],
    )
klapzL commented 6 months ago

I have the same, and my error looks like this

500 Server Error
ImportError: cannot import name 'AsyncExitStack' from 'fastapi.concurrency' (D:\Python\projects\eventic-fastapi\env\Lib\site-packages\fastapi\concurrency.py)

Traceback:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 412, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\uvicorn\middleware\proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\fastapi\applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\applications.py", line 123, in __call__
    await self.middleware_stack(scope, receive, send)
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\middleware\errors.py", line 186, in __call__
    raise exc
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\middleware\errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\middleware\base.py", line 189, in __call__
    with collapse_excgroups():
  File "C:\Users\klern\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(value)
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\_utils.py", line 93, in collapse_excgroups
    raise exc
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\starlette\middleware\base.py", line 191, in __call__
    response = await self.dispatch_func(request, call_next)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\debug_toolbar\middleware.py", line 74, in dispatch
    toolbar = DebugToolbar(request, call_next, self.settings)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\debug_toolbar\toolbar.py", line 34, in __init__
    for panel_class in self.get_panel_classes(
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\debug_toolbar\toolbar.py", line 60, in get_panel_classes
    cls._panel_classes = [import_string(panel_path) for panel_path in panels]
                          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\debug_toolbar\utils.py", line 23, in import_string
    module = __import__(module_name, globals(), locals(), [obj_name])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Python\projects\eventic-fastapi\env\Lib\site-packages\debug_toolbar\panels\sqlalchemy.py", line 5, in <module>
    from fastapi.concurrency import AsyncExitStack
ImportError: cannot import name 'AsyncExitStack' from 'fastapi.concurrency' (D:\Python\projects\eventic-fastapi\env\Lib\site-packages\fastapi\concurrency.py)
klapzL commented 6 months ago

I solved it by just downgrading FastAPI to 0.100.1

anshidnm commented 6 months ago

I solved it by just downgrading FastAPI to 0.100.1

The issue you mentioned also faced to me earlier. Then I downgrade FastAPI to 0.105. Then solved that issue. But still occur the issue while if any exception raise from dependency (current user) which I mentioned first.

klapzL commented 6 months ago

Oh, I see. Didn't notice the dependency you mentioned. I'll try it

fsecada01 commented 6 months ago

https://github.com/mongkok/fastapi-debug-toolbar/pull/41 should resolve the issue. You can ping the maintainer to get him to approve it.

anshidnm commented 6 months ago

The issue still exists

I installed the latest version by

pip install git+https://github.com/mongkok/fastapi-debug-toolbar

But still if any exception raised from dependency that causes an internal server error while below middleware using. especially the panels=["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"]

app.add_middleware(
        DebugToolbarMiddleware,
        panels=["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"],
    )
mongkok commented 6 months ago

@anshidnm please share the error and FastAPI version.

EDIT: My bad, 0.6.0 is wrongly tagged, please check it again.

anshidnm commented 6 months ago

@anshidnm please share the error and FastAPI version.

~EDIT: My bad, 0.6.0 is wrongly tagged, please check it again.~

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 164, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.10/site-packages/starlette/middleware/base.py", line 191, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/middleware.py", line 75, in dispatch
    response = t.cast(StreamingResponse, await toolbar.process_request(request))
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/timer.py", line 58, in process_request
    return await super().process_request(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  [Previous line repeated 2 more times]
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/logging.py", line 96, in process_request
    return await super().process_request(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/profiling.py", line 26, in process_request
    response = await super().process_request(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/__init__.py", line 79, in process_request
    return await self.call_next(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/sqlalchemy.py", line 63, in process_request
    await self.add_engines(request)
  File "/usr/local/lib/python3.10/site-packages/debug_toolbar/panels/sqlalchemy.py", line 47, in add_engines
    solved_result = await solve_dependencies(
  File "/usr/local/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 570, in solve_dependencies
    solved_result = await solve_dependencies(
  File "/usr/local/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 598, in solve_dependencies
    solved = await call(**sub_values)
  File "/app/auth/jwt_tokens.py", line 92, in __call__
    raise_exception(
  File "/app/utils.py", line 48, in raise_exception
    raise HTTPException(
fastapi.exceptions.HTTPException: 401: Authentication credentials not provided

This ISE was still occur even if i updated the fastapi-debug-toolbar version to 6.0.1. Like i said before without panels=["debug_toolbar.panels.sqlalchemy.SQLAlchemyPanel"] then instead of ISE the api return with 401 status.

anshidnm commented 6 months ago

my requirements.txt

alembic==1.13.1
annotated-types==0.6.0
anyio==3.7.1
click==8.1.7
dnspython==2.5.0
ecdsa==0.18.0
email-validator==2.1.0.post1
exceptiongroup==1.2.0
fastapi==0.109.2
fastapi-debug-toolbar @ git+https://github.com/mongkok/fastapi-debug-toolbar@1812a3e5d71d7740a42e0e0fbd7956a265f7a085
greenlet==3.0.3
h11==0.14.0
idna==3.6
Jinja2==3.1.3
Mako==1.3.0
MarkupSafe==2.1.4
psycopg2==2.9.9
pyasn1==0.5.1
pydantic==2.6.0
pydantic-extra-types==2.5.0
pydantic-settings==2.1.0
pydantic_core==2.16.1
pyinstrument==4.6.2
python-dotenv==1.0.1
python-jose==3.3.0
python-multipart==0.0.9
rsa==4.9
six==1.16.0
sniffio==1.3.0
SQLAlchemy==2.0.25
sqlparse==0.4.4
starlette==0.36.3
typing_extensions==4.9.0
uvicorn==0.27.0.post1
mongkok commented 6 months ago

Thanks @anshidnm , I've added an HTTPException handler. A 401 error is returned in case the request is not authenticated, please check the new release.

anshidnm commented 6 months ago

Thanks @mongkok its working now