Better 500 template (show exception, point to logs)

This commit is contained in:
Klaas van Schelven
2024-11-28 12:33:02 +01:00
parent 2a0580cb4e
commit e7ddfa0855
3 changed files with 39 additions and 2 deletions

View File

@@ -63,3 +63,6 @@ if settings.DEBUG:
path('trigger-error/', trigger_error),
path("__debug__/", include("debug_toolbar.urls")),
]
handler500 = "bugsink.views.server_error"

View File

@@ -1,3 +1,9 @@
import sys
from django.http import HttpResponseServerError
from django.template import TemplateDoesNotExist, loader
from django.views.decorators.csrf import requires_csrf_token
from django.views.defaults import ERROR_500_TEMPLATE_NAME, ERROR_PAGE_TEMPLATE
from django.shortcuts import redirect
from django.conf import settings
@@ -108,3 +114,22 @@ def settings_view(request):
"misc_settings": misc_settings,
"version": __version__,
})
@requires_csrf_token
def server_error(request, template_name=ERROR_500_TEMPLATE_NAME):
# verbatim copy of Django's default server_error view, but with "exception" in the context
# doing this for any-old-Django-site is probably a bad idea, but here the security/convenience tradeoff is fine,
# especially because we only show str(exception) in the template.
try:
template = loader.get_template(template_name)
except TemplateDoesNotExist:
if template_name != ERROR_500_TEMPLATE_NAME:
# Reraise if it's a missing custom template.
raise
return HttpResponseServerError(
ERROR_PAGE_TEMPLATE % {"title": "Server Error (500)", "details": ""},
)
_, exception, _ = sys.exc_info()
return HttpResponseServerError(template.render({"exception": exception}, request))

View File

@@ -6,7 +6,16 @@
<div class="m-4">
<h1 class="text-4xl mt-4 font-bold">500 Server Error</h1>
<div class="pt-2">
There was an internal server error.
<div class="pt-2 font-bold">
{{ exception }}
</div>
<div class="pt-6">
You will find more information in:
<ul class="list-disc pt-4 pl-4">
<li>the server logs (stdout of your Docker, or in journalctl)</li>
<li>if you're <a href="https://www.bugsink.com/docs/dogfooding/" class="text-cyan-500 font-bold">dogfooding Bugsink</a>, in Bugsink itself</li>
</ul>
</div>
{% endblock %}