Coder Social home page Coder Social logo

Comments (5)

sondrelg avatar sondrelg commented on September 24, 2024

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 🙏

from django-guid.

andrew-cybsafe avatar andrew-cybsafe commented on September 24, 2024

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?

from django-guid.

sondrelg avatar sondrelg commented on September 24, 2024

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 👏

from django-guid.

JonasKs avatar JonasKs commented on September 24, 2024

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

from django-guid.

sondrelg avatar sondrelg commented on September 24, 2024

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

from django-guid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.