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 a 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
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/
Licence
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
Hashes for drf-standardized-errors-0.12.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44e5f6fa1eab3c16563972dc5de26cc4acf4ea117f7c21d4feb8b66b7e1c70eb |
|
MD5 | eb085c2f6b582d16df1f7421f033fba9 |
|
BLAKE2b-256 | 59ac0ddec09d29f093f7d1ba36bd54dad16746d0e3f28b7838aa6e6a27fdf912 |
Hashes for drf_standardized_errors-0.12.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7876f22375b4f136b9a01f2fa0c369fe78b184eff2d9f5ff59f03be238c5bc02 |
|
MD5 | 49f9c7eac894deeb4d81796b3ce1678a |
|
BLAKE2b-256 | dd24e6d7c6135b42ac9fd86e2e9b9262ff06987a9d2fa87433ef67855e1d032c |