Starlette: The debug mode does not work well

Created on 18 Jan 2021  路  2Comments  路  Source: encode/starlette

Checklist

  • [x] The bug is reproducible against the latest release and/or master.
  • [x] There are no similar issues or pull requests to fix it yet.

Describe the bug

The application shows plain text instead of html page in debug mode when an error occurs.

To reproduce

I am trying to use the debug mode. Here is my application:

from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route


async def show_index(request):
    return JSONResponse({'hello': current_user})


app = Starlette(debug=True, routes=[
    Route('/', show_index)])

I start the server this way:

import uvicorn

if __name__ == '__main__':
    uvicorn.run(
        'main:app', host='127.0.0.1',
        reload=True, port=5000, log_level='info')

Expected behavior

I expect an exception and the html page in my browser with debug information, but it shows plain text:
'Internal Server Error'.

Actual behavior

Browser shows plain text: 'Internal Server Error'

Debugging material

This is the traceback I see in terminal:

Traceback (most recent call last):
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/routing.py", line 582, in __call__
    await route.handle(scope, receive, send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/routing.py", line 243, in handle
    await self.app(scope, receive, send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/routing.py", line 54, in app
    response = await func(request)
  File "/home/avm/workspace/abug/main.py", line 7, in show_index
    return JSONResponse({'hello': current_user})
NameError: name 'current_user' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 394, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 165, in __call__
    response = self.debug_response(request, exc)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 245, in debug_response
    content = self.generate_html(exc)
  File "/home/avm/workspace/abug/venv/lib/python3.9/site-packages/starlette/middleware/errors.py", line 221, in generate_html
    traceback_obj.exc_traceback, limit  # type: ignore
AttributeError: 'TracebackException' object has no attribute 'exc_traceback'
INFO:     127.0.0.1:48170 - "GET / HTTP/1.1" 500 Internal Server Error

Environment

  • OS: Debian bullseye
  • Python version: Python 3.9.1+
  • Starlette version: starlette==0.14.1

Additional context

It looks like something bad happens in middleware/errors.py - line 221. So... Is it a bug or am I doing something wrong and illegal?

Most helpful comment

I have the same issue. exc_traceback got removed in Python 3.9 and 3.8.7 because it being there was wrong (https://bugs.python.org/issue42482).
Replacing traceback_obj.exc_traceback on line 221 with exc.__traceback__ works (it's the same object exc_traceback used to point at) but has the same issue because of which exc_traceback got removed in the first place, namely holding references.

All 2 comments

I have the same issue. exc_traceback got removed in Python 3.9 and 3.8.7 because it being there was wrong (https://bugs.python.org/issue42482).
Replacing traceback_obj.exc_traceback on line 221 with exc.__traceback__ works (it's the same object exc_traceback used to point at) but has the same issue because of which exc_traceback got removed in the first place, namely holding references.

This should be fixed in Starlette 0.14.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

berislavlopac picture berislavlopac  路  5Comments

rlewkowicz picture rlewkowicz  路  3Comments

hyperknot picture hyperknot  路  6Comments

Serkan-devel picture Serkan-devel  路  5Comments

jordic picture jordic  路  4Comments