Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on non-text views #1959

Closed
nijel opened this issue Jul 9, 2024 · 0 comments · Fixed by #1960
Closed

Crash on non-text views #1959

nijel opened this issue Jul 9, 2024 · 0 comments · Fixed by #1960

Comments

@nijel
Copy link
Contributor

nijel commented Jul 9, 2024

On view that generates a PNG image, django-debug-toolbar alert panel crashes:

Traceback (most recent call last):
  File "/home/nijel/weblate/weblate/.venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/nijel/weblate/weblate/.venv/lib/python3.11/site-packages/sentry_sdk/integrations/django/middleware.py", line 169, in __call__
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/home/nijel/weblate/weblate/.venv/lib/python3.11/site-packages/debug_toolbar/middleware.py", line 94, in __call__
    panel.generate_stats(request, response)
  File "/home/nijel/weblate/weblate/.venv/lib/python3.11/site-packages/debug_toolbar/panels/alerts.py", line 146, in generate_stats
    html_content = response.content.decode(response.charset)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

The response is <HttpResponse status_code=200, "image/png"> and doesn't have charset set. In this case charset property falls back to the default, what happens to be 'utf-8'.

I think it should check response.headers["Content-Type"] if the response is something what makes sense to decode before doing so here:

https://github.com/jazzband/django-debug-toolbar/blob/9bcd6cac17fd1721d60c2b1b8218884caf0ee45e/debug_toolbar/panels/alerts.py#L145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant