DRF Standardized Errors
drf-standardized-errors is a Python library for Django REST Framework that standardizes API error responses, making them consistent and easier for clients to consume. It is currently at version 0.15.0 and maintains an active release cadence, frequently adding support for new Python, Django, and DRF versions.
Warnings
- breaking As of v0.15.0, unhandled exceptions now return a generic error message by default to prevent sensitive data leaks. This changes the previous behavior of exposing the raw exception message.
- breaking If using `drf-spectacular` (versions 0.27.0 or newer), the `SPECTACULAR_SETTINGS["ENUM_NAME_OVERRIDES"]` entries for 'ValidationErrorEnum' and 'ClientErrorEnum' must reference `choices` instead of `values`.
- gotcha Improper configuration of `REST_FRAMEWORK['EXCEPTION_HANDLER']` in `settings.py` will prevent `drf-standardized-errors` from taking effect, resulting in default DRF error responses.
- gotcha Compatibility with `drf-spectacular` for OpenAPI schema generation features is often tied to specific `drf-spectacular` versions. Refer to the release notes for precise version requirements to ensure correct schema generation (e.g., v0.14.0 enforces `drf-spectacular >=0.27`).
Install
-
pip install drf-standardized-errors
Imports
- exception_handler
from drf_standardized_errors.handler import exception_handler
- DRFStandardizedErrorsExceptionHandler
from drf_standardized_errors.handler import DRFStandardizedErrorsExceptionHandler
Quickstart
# settings.py
REST_FRAMEWORK = {
"EXCEPTION_HANDLER": "drf_standardized_errors.handler.exception_handler"
}