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

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

Closed
andrew-cybsafe opened this issue Jul 14, 2022 · 5 comments
Closed
Labels
suggestion A feature request/suggestion

Comments

@andrew-cybsafe
Copy link

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.

@andrew-cybsafe andrew-cybsafe added the suggestion A feature request/suggestion label Jul 14, 2022
@sondrelg
Copy link
Member

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 🙏

@andrew-cybsafe
Copy link
Author

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?

@sondrelg
Copy link
Member

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 👏

@JonasKs
Copy link
Member

JonasKs commented Jul 14, 2022

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

@sondrelg
Copy link
Member

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

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

No branches or pull requests

3 participants