Science Score: 44.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (10.9%) to scientific vocabulary
Keywords
Repository
Django Swing | Error
Basic Info
- Host: GitHub
- Owner: swing-collection
- License: bsd-3-clause
- Language: Python
- Default Branch: dev
- Homepage: https://www.swing.dj
- Size: 1.95 MB
Statistics
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 0
Topics
Metadata Files
README.md
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
- Clone the repository to your local machine.
- 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:
```python
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.
```python
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 homeview, anotherview
urlpatterns = [ path('', homeview, name='home'), path('another/', anotherview, 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):
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:
```python from django.shortcuts import render from .responses import Http400Response, Http404Response
def someview(request): if somecondition: return Http400Response("Bad Request: Invalid data.") if anothercondition: return Http404Response("Not Found: The requested resource was not found.") return render(request, 'sometemplate.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
- https://docs.djangoproject.com/en/stable/howto/error-reporting/
- https://docs.djangoproject.com/en/stable/ref/urls/#django.conf.urls.handler400
Templates
- https://codepen.io/akashrajendra/pen/JKKRvQ
https://webartdevelopers.com/blog/category/500-error-page-html-templates/
https://github.com/wooyek/django-error-views
errorhandler/ ├── _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
Owner
- Name: Swing Collection
- Login: swing-collection
- Kind: organization
- Website: https://www.swing.dj
- Repositories: 15
- Profile: https://github.com/swing-collection
Django Packages
Citation (CITATION.cff)
cff-version: 1.2.0
title: swing-error
version: 0.2.1
date-released: 2024-07-01
url: "https://github.com/swing-collection/swing-error"
message: >-
If you use this software, please cite it using
the metadata from this file.
type: software
authors:
- given-names: Lars Bastiaan
family-names: van Vianen
email: lars@scape.agency
affiliation: Scape Agency
orcid: 'https://orcid.org/0000-0002-8790-8630'
GitHub Events
Total
- Delete event: 33
- Issue comment event: 31
- Push event: 53
- Pull request event: 66
- Create event: 35
Last Year
- Delete event: 33
- Issue comment event: 31
- Push event: 53
- Pull request event: 66
- Create event: 35
Issues and Pull Requests
Last synced: 4 months ago
All Time
- Total issues: 0
- Total pull requests: 52
- Average time to close issues: N/A
- Average time to close pull requests: 2 days
- Total issue authors: 0
- Total pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 1.04
- Merged pull requests: 51
- Bot issues: 0
- Bot pull requests: 52
Past Year
- Issues: 0
- Pull requests: 48
- Average time to close issues: N/A
- Average time to close pull requests: 2 days
- Issue authors: 0
- Pull request authors: 1
- Average comments per issue: 0
- Average comments per pull request: 1.0
- Merged pull requests: 48
- Bot issues: 0
- Bot pull requests: 48
Top Authors
Issue Authors
- dependabot[bot] (1)
Pull Request Authors
- dependabot[bot] (53)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- actions/checkout v4 composite
- actions/setup-python v5 composite
- python 3.12-slim build
- black ^24.8.0 develop
- flake8 ^7.1.0 develop
- isort ^5.10.1 develop
- mypy ^1.11.1 develop
- pytest ^8.3.2 develop
- pytest-cov ^5.0.0 develop
- sphinx ^8.0.2 develop
- Django ^4.2
- asgiref >=3.8.1
- python ^3.8
- sqlparse >=0.3.1
- tzdata sys_platform == 'win32'
- dependabot/fetch-metadata v1 composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- Django >=5.1
- black *
- flake8 *
- mypy *
- pytest *
- pytest-django *
- sphinx *
- sphinx-rtd-theme *
- tox *