Standardize your API error responses.
Project description
DRF Standardized Errors
Standardize your DRF API error responses.
By default, the package will convert all API error responses (4xx and 5xx) to the following standardized format:
{
"type": "validation_error",
"errors": [
{
"code": "required",
"detail": "This field is required.",
"attr": "name"
},
{
"code": "max_length",
"detail": "Ensure this value has at most 100 characters.",
"attr": "title"
}
]
}
{
"type": "client_error",
"errors": [
{
"code": "authentication_failed",
"detail": "Incorrect authentication credentials.",
"attr": null
}
]
}
{
"type": "server_error",
"errors": [
{
"code": "error",
"detail": "A server error occurred.",
"attr": null
}
]
}
Features
- Highly customizable: gives you flexibility to define your own standardized error responses and override specific aspects the exception handling process without having to rewrite everything.
- Supports nested serializers and ListSerializer errors
- Plays nicely with error monitoring tools (like Sentry, ...)
Requirements
- python >= 3.8
- Django >= 3.2
- DRF >= 3.12
Quickstart
Install with pip
pip install drf-standardized-errors
Add drf-standardized-errors to your installed apps
INSTALLED_APPS = [
# other apps
"drf_standardized_errors",
]
Register the exception handler
REST_FRAMEWORK = {
# other settings
"EXCEPTION_HANDLER": "drf_standardized_errors.handler.exception_handler"
}
Notes
-
This package is a DRF exception handler, so it standardizes errors that reach a DRF API view. That means it cannot handle errors that happen at the middleware level for example. To handle those as well, you can customize the necessary django error views. You can find more about that in this issue.
-
Standardized error responses when
DEBUG=True
for unhandled exceptions are disabled by default. That is to allow you to get more information out of the traceback. You can enable standardized errors instead with:
DRF_STANDARDIZED_ERRORS = {"ENABLE_IN_DEBUG_FOR_UNHANDLED_EXCEPTIONS": True}
Integration with DRF spectacular
If you plan to use drf-spectacular to generate an OpenAPI 3 schema,
install with pip install drf-standardized-errors[openapi]
. After that, check the doc page
for configuring the integration.
Links
- Documentation: https://drf-standardized-errors.readthedocs.io/en/latest/
- Changelog: https://github.com/ghazi-git/drf-standardized-errors/releases
- Code & issues: https://github.com/ghazi-git/drf-standardized-errors
- PyPI: https://pypi.org/project/drf-standardized-errors/
License
This project is MIT licensed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file drf_standardized_errors-0.14.1.tar.gz
.
File metadata
- Download URL: drf_standardized_errors-0.14.1.tar.gz
- Upload date:
- Size: 58.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0610dcd0096b75365102d276022a22e59a1f8db8825bb0bff05e1b7194ba145d |
|
MD5 | 5050ab078a393cd9000a0460abc65990 |
|
BLAKE2b-256 | 4dccfd5b8cbc66c361125cba0497573a5ecac94521a715267d7db4d113257a73 |
File details
Details for the file drf_standardized_errors-0.14.1-py3-none-any.whl
.
File metadata
- Download URL: drf_standardized_errors-0.14.1-py3-none-any.whl
- Upload date:
- Size: 25.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.31.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4941e0f81be94eb0904549999cf221988a5b0f524041c3877530e24f70328ed8 |
|
MD5 | 6fdddf8fedab7dfd5df62def55a2139e |
|
BLAKE2b-256 | 4470589efc32d6f268576e2f3c2a595ef19a305c5d5acbfd26d10ebd45278778 |