{"id":21574411,"url":"https://github.com/swing-collection/swing-error","last_synced_at":"2026-01-23T15:42:24.356Z","repository":{"id":252921140,"uuid":"821706496","full_name":"swing-collection/swing-error","owner":"swing-collection","description":"Django Swing | Error","archived":false,"fork":false,"pushed_at":"2025-12-14T07:03:06.000Z","size":2389,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":0,"default_branch":"dev","last_synced_at":"2025-12-16T10:25:26.655Z","etag":null,"topics":["400","403","404","500","django","error","error-handling","errors","swing","swing-collection"],"latest_commit_sha":null,"homepage":"https://www.swing.dj","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/swing-collection.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"scape-foundation","open_collective":"scape"}},"created_at":"2024-06-29T07:50:59.000Z","updated_at":"2025-12-14T07:03:04.000Z","dependencies_parsed_at":"2024-12-02T09:25:18.058Z","dependency_job_id":"5943998b-8048-48f9-926a-53903abcee16","html_url":"https://github.com/swing-collection/swing-error","commit_stats":null,"previous_names":["swing-collection/swing-error"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/swing-collection/swing-error","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swing-collection%2Fswing-error","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swing-collection%2Fswing-error/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swing-collection%2Fswing-error/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swing-collection%2Fswing-error/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swing-collection","download_url":"https://codeload.github.com/swing-collection/swing-error/tar.gz/refs/heads/dev","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swing-collection%2Fswing-error/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28694823,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T14:15:13.573Z","status":"ssl_error","status_checked_at":"2026-01-23T14:09:05.534Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["400","403","404","500","django","error","error-handling","errors","swing","swing-collection"],"created_at":"2024-11-24T12:09:42.858Z","updated_at":"2026-01-23T15:42:24.338Z","avatar_url":"https://github.com/swing-collection.png","language":"Python","funding_links":["https://github.com/sponsors/scape-foundation","https://opencollective.com/scape"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/scape-agency/swing.dj/blob/85830584264bca52c02e1f0dcfa3648f84783805/res/swing-logo.png\" width=\"20%\" height=\"20%\" alt=\"Django Swing Logo\"\u003e\n\u003c/p\u003e\n\u003ch1 align='center' style='border-bottom: none;'\u003eSwing Error\u003c/h1\u003e\n\u003ch3 align='center'\u003eDjango Swing Collection\u003c/h3\u003e\n\u003cbr/\u003e\n\n## Overview\n\n**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:\n\n- 400 Bad Request\n- 401 Unauthorized\n- 403 Forbidden\n- 404 Not Found\n- 405 Method Not Allowed\n- 408 Request Timeout\n- 410 Gone\n- 429 Too Many Requests\n- 500 Internal Server Error\n\n## Installation\n\n1. Clone the repository to your local machine.\n2. Add the custom error handlers to your Django project.\n\n## Setup\n\n### Step 1: Define Custom Response Classes\n\nCreate a file named `responses.py` in your Django application directory and define custom response classes for each HTTP status code.\n\nExample for HTTP 400:\n\n```python\n# responses.py\nfrom django.http import HttpResponse\nfrom typing import Any, Union\nimport logging\n\nclass Http400Response(HttpResponse):\n    status_code = 400\n\n    def __init__(self, content: Union[bytes, str] = b'', *args: Any, **kwargs: Any) -\u003e None:\n        super().__init__(content, *args, **kwargs)\n        self.log_error()\n\n    def log_error(self) -\u003e None:\n        logger = logging.getLogger(__name__)\n        logger.error(f\"400 Bad Request: Response initialized with content: {self.content}\")\n```\n\nRepeat this for other status codes (401, 403, 404, 405, 408, 410, 429, 500) as shown in the initial setup.\n\n### Step 2: Update URL Configuration\n\nUpdate your `urls.py` file to include the custom error handlers.\n\n```python\n# urls.py\nfrom django.urls import path\nfrom django.conf.urls import handler400, handler401, handler403, handler404, handler405, handler408, handler410, handler429, handler500\nfrom .responses import (\n    Http400Response, Http401Response, Http403Response, Http404Response,\n    Http405Response, Http408Response, Http410Response, Http429Response, Http500Response\n)\nfrom .views import home_view, another_view\n\nurlpatterns = [\n    path('', home_view, name='home'),\n    path('another/', another_view, name='another'),\n]\n\nhandler400 = lambda request, exception=None: Http400Response(\"Bad Request: Invalid request.\")\nhandler401 = lambda request, exception=None: Http401Response(\"Unauthorized: Authentication is required.\")\nhandler403 = lambda request, exception=None: Http403Response(\"Forbidden: You do not have permission to access this page.\")\nhandler404 = lambda request, exception=None: Http404Response(\"Not Found: The requested resource was not found.\")\nhandler405 = lambda request, exception=None: Http405Response(\"Method Not Allowed: This endpoint only supports certain methods.\")\nhandler408 = lambda request, exception=None: Http408Response(\"Request Timeout: The server timed out waiting for the request.\")\nhandler410 = lambda request, exception=None: Http410Response(\"Gone: The requested resource is no longer available.\")\nhandler429 = lambda request, exception=None: Http429Response(\"Too Many Requests: You have exceeded your request limit.\")\nhandler500 = lambda request: Http500Response(\"Internal Server Error: An unexpected error occurred.\")\n```\n\n### Step 3: Create Custom Templates\n\nCreate custom templates for each error handler in your templates directory.\n\nExample for 400 error (templates/errors/400.html):\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n\u003chead\u003e\n    \u003ctitle\u003e{{ title }}\u003c/title\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003e{{ header }}\u003c/h1\u003e\n    \u003cp\u003e{{ message }}\u003c/p\u003e\n    \u003ca href=\"/\"\u003e{{ redirect }}\u003c/a\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nRepeat this for other status codes (401, 403, 404, 405, 408, 410, 429, 500) with appropriate content.\n\n### Step 4: Test Custom Error Handlers\n\nEnsure 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.\n\n## Usage\n\nIn your views, you can use the custom response classes to return specific error responses as needed. For example:\n\n```python\nfrom django.shortcuts import render\nfrom .responses import Http400Response, Http404Response\n\ndef some_view(request):\n    if some_condition:\n        return Http400Response(\"Bad Request: Invalid data.\")\n    if another_condition:\n        return Http404Response(\"Not Found: The requested resource was not found.\")\n    return render(request, 'some_template.html')\n```\n\n\n---\n\n## Colophon\n\nMade with ❤️ by **[Scape Agency](https://www.scape.agency)**\n\n### Contributing\n\nContributions are welcome! Please fork the repository and submit a pull request with your changes.\n\n### License\n\nThis project is licensed under the BSD-3-Clause license. See the [LICENSE](LICENSE) file for details.\n\n---\n\n\n\n\nError Handler App for Django\n\n## Links\n\n### Docs\n\n- https://docs.djangoproject.com/en/stable/howto/error-reporting/\n- https://docs.djangoproject.com/en/stable/ref/urls/#django.conf.urls.handler400\n\n### Templates\n\n- https://codepen.io/akashrajendra/pen/JKKRvQ\n- https://webartdevelopers.com/blog/category/500-error-page-html-templates/\n\n- https://github.com/wooyek/django-error-views\n\n\n\n\n\nerror_handler/\n    ├── __init__.py\n    ├── admin.py\n    ├── apps.py\n    ├── handlers.py       # Error handling logic\n    ├── middleware.py     # Middleware for global error capture\n    ├── models.py\n    ├── templates/        # Custom error pages (if HTML responses)\n    │   └── error.html\n    ├── tests.py\n    ├── urls.py           # Routes for testing error responses\n    └── views.py          # Optional views for error simulation","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswing-collection%2Fswing-error","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswing-collection%2Fswing-error","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswing-collection%2Fswing-error/lists"}