Skip to main content

A Django plugin that manages error handling and custom error pages.

Project description

Django Swing Logo

Swing Error

Django Swing Collection


Overview

Swing Error provides custom error handlers for various HTTP status codes in a Django application. Each error handler is designed to return a custom response with additional functionality and logging capabilities. The custom error handlers cover the following HTTP status codes:

  • 400 Bad Request
  • 401 Unauthorized
  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 408 Request Timeout
  • 410 Gone
  • 429 Too Many Requests
  • 500 Internal Server Error

Installation

  1. Clone the repository to your local machine.
  2. Add the custom error handlers to your Django project.

Setup

Step 1: Define Custom Response Classes

Create a file named responses.py in your Django application directory and define custom response classes for each HTTP status code.

Example for HTTP 400:

# responses.py
from django.http import HttpResponse
from typing import Any, Union
import logging

class Http400Response(HttpResponse):
    status_code = 400

    def __init__(self, content: Union[bytes, str] = b'', *args: Any, **kwargs: Any) -> None:
        super().__init__(content, *args, **kwargs)
        self.log_error()

    def log_error(self) -> None:
        logger = logging.getLogger(__name__)
        logger.error(f"400 Bad Request: Response initialized with content: {self.content}")

Repeat this for other status codes (401, 403, 404, 405, 408, 410, 429, 500) as shown in the initial setup.

Step 2: Update URL Configuration

Update your urls.py file to include the custom error handlers.

# urls.py
from django.urls import path
from django.conf.urls import handler400, handler401, handler403, handler404, handler405, handler408, handler410, handler429, handler500
from .responses import (
    Http400Response, Http401Response, Http403Response, Http404Response,
    Http405Response, Http408Response, Http410Response, Http429Response, Http500Response
)
from .views import home_view, another_view

urlpatterns = [
    path('', home_view, name='home'),
    path('another/', another_view, name='another'),
]

handler400 = lambda request, exception=None: Http400Response("Bad Request: Invalid request.")
handler401 = lambda request, exception=None: Http401Response("Unauthorized: Authentication is required.")
handler403 = lambda request, exception=None: Http403Response("Forbidden: You do not have permission to access this page.")
handler404 = lambda request, exception=None: Http404Response("Not Found: The requested resource was not found.")
handler405 = lambda request, exception=None: Http405Response("Method Not Allowed: This endpoint only supports certain methods.")
handler408 = lambda request, exception=None: Http408Response("Request Timeout: The server timed out waiting for the request.")
handler410 = lambda request, exception=None: Http410Response("Gone: The requested resource is no longer available.")
handler429 = lambda request, exception=None: Http429Response("Too Many Requests: You have exceeded your request limit.")
handler500 = lambda request: Http500Response("Internal Server Error: An unexpected error occurred.")

Step 3: Create Custom Templates

Create custom templates for each error handler in your templates directory.

Example for 400 error (templates/errors/400.html):

<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ header }}</h1>
    <p>{{ message }}</p>
    <a href="/">{{ redirect }}</a>
</body>
</html>

Repeat this for other status codes (401, 403, 404, 405, 408, 410, 429, 500) with appropriate content.

Step 4: Test Custom Error Handlers

Ensure that the custom error handlers are invoked correctly by triggering the respective errors in your application. For example, you can test a 404 error by accessing a non-existent URL.

Usage

In your views, you can use the custom response classes to return specific error responses as needed. For example:

from django.shortcuts import render
from .responses import Http400Response, Http404Response

def some_view(request):
    if some_condition:
        return Http400Response("Bad Request: Invalid data.")
    if another_condition:
        return Http404Response("Not Found: The requested resource was not found.")
    return render(request, 'some_template.html')

Colophon

Made with ❤️ by Scape Agency

Contributing

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

License

This project is licensed under the BSD-3-Clause license. See the LICENSE file for details.


Error Handler App for Django

Links

Docs

Templates

error_handler/ ├── init.py ├── admin.py ├── apps.py ├── handlers.py # Error handling logic ├── middleware.py # Middleware for global error capture ├── models.py ├── templates/ # Custom error pages (if HTML responses) │ └── error.html ├── tests.py ├── urls.py # Routes for testing error responses └── views.py # Optional views for error simulation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swing_error-0.2.1.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swing_error-0.2.1-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file swing_error-0.2.1.tar.gz.

File metadata

  • Download URL: swing_error-0.2.1.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for swing_error-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2ab3e05fa98a3f9946224f29a0b71493910c9f7cbfc05cac53ab90df2d76e5ab
MD5 cf7f757a18a21479b420fb541e0cebb0
BLAKE2b-256 a1c631dc038b1a49d66ac734d8cd739af6027593cf4e0dfc57963f143faa6f22

See more details on using hashes here.

File details

Details for the file swing_error-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: swing_error-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 44.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for swing_error-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c12140341c3e35fee37c9d47f480c21e158412a375ec069c9c92cb4a7920353f
MD5 a40021e9354beb6decd8c3467ba81ca5
BLAKE2b-256 e3ca7814c70cad7c3c027392d88b33ad8445a25ac93c2ae7cdd758d42418aa5a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page