master.The application shows plain text instead of html page in debug mode when an error occurs.
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')
I expect an exception and the html page in my browser with debug information, but it shows plain text:
'Internal Server Error'.
Browser shows plain text: 'Internal Server Error'
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
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?
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
Most helpful comment
I have the same issue.
exc_tracebackgot removed in Python 3.9 and 3.8.7 because it being there was wrong (https://bugs.python.org/issue42482).Replacing
traceback_obj.exc_tracebackon line 221 withexc.__traceback__works (it's the same objectexc_tracebackused to point at) but has the same issue because of whichexc_tracebackgot removed in the first place, namely holding references.