snok / django-guid

Inject an ID into every log message from a Django request. ASGI compatible, integrates with Sentry, and works with Celery

Home Page:https://django-guid.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide error page views that add the guid to the HTML output

andrew-cybsafe opened this issue · comments

I would like an easy way to override the Django error page views so that the request guid is included as part of the HTML. This is useful so that customers can easily send the guid to a support team to get faster help.

You should read about handler500. You can very simply add a callable like this:

def handler500(request):
    return TemplateResponse(request, '500.html', {'correlation_id': correlation_id.get()}, status=500)

where the 500.html contains the HTML you wish to render 👍

Please feel free to report back with a solution when you're done. Could be cool to add a generalised example to the docs 🙏

Here's what worked for me

New config/error_views.py:

from django.http import (
    HttpResponseBadRequest,
    HttpResponseForbidden,
    HttpResponseNotFound,
    HttpResponseServerError,
)
from django_guid import get_guid

ERROR_PAGE_TEMPLATE = """
<!doctype html>
<html lang="en">
<head>
  <title>%(title)s</title>
</head>
<body>
  <h1>%(title)s</h1>
  <p>%(details)s</p>
  <p>Request ID: %(guid)s</p>
</body>
</html>
"""


def server_error(request):
    return HttpResponseServerError(
        ERROR_PAGE_TEMPLATE
        % {
            "title": "Server Error (500)",
            "details": "An unexpected error occurred whilst processing the request",
            "guid": get_guid(),
        }
    )


def permission_denied(request, exception, template_name="403.html"):
    return HttpResponseForbidden(
        ERROR_PAGE_TEMPLATE
        % {
            "title": "Permission Denied (403)",
            "details": "No access to requested resource",
            "guid": get_guid(),
        }
    )


def bad_request(request, exception, template_name="400.html"):
    return HttpResponseBadRequest(
        ERROR_PAGE_TEMPLATE
        % {
            "title": "Bad Request (400)",
            "details": "",
            "guid": get_guid(),
        }
    )

In config/urls.py add:

handler500 = "config.error_views.server_error"
handler403 = "config.error_views.permission_denied"
handler400 = "config.error_views.bad_request"

Would you be amenable to including the "default with guid" error handlers to the package?

I'll leave that up to @JonasKs to decide, but I think adding template code to the library is a little out-of-scope. Think the styling etc. should be up to the user to define. Think it's a nice thing to describe how to set up in the docs though 👏

Agree with @sondrelg 😊 Documentation PR very welcome, but I'd prefer not to maintain templates - styling is so different between everyone.

I'll close this for now. If anyone else runs into similar issues, we now at least have your example to reference @andrew-cybsafe 👏