Skip to main content

DRF Standardized Errors

Standardize your DRF API error responses.

Read the Docs GitHub Workflow Status codecov PyPI PyPI - License Code style: black

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

License

This project is MIT licensed.

Metadata

Release files for drf-standardized-errors 0.16.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-standardized-errors 0.16.0
File Size Uploaded
drf_standardized_errors-0.16.0.tar.gz 62.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-standardized-errors 0.16.0
File Interpreter ABI Platform
drf_standardized_errors-0.16.0-py3-none-any.whl Python 3 none any Details

Total release size: 88.2 kB

Release files / drf_standardized_errors-0.16.0.tar.gz

Download URL drf_standardized_errors-0.16.0.tar.gz
Size 62.1 kB
Tags Source
SHA-256 checksum
How to use checksums
77b367955af7ed246db12d299a5a75a71eccef54dae4ef69ead29f5fb01be192
BLAKE2b-256 checksum
How to use checksums
993f1a465bde6583355309559dfcfb4e7fdf1b3687f73894d7de8a959c67b50c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release files / drf_standardized_errors-0.16.0-py3-none-any.whl

Download URL drf_standardized_errors-0.16.0-py3-none-any.whl
Size 26.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf0ef3120cbbe81c1880cbb6b2d9b26cc6d146d07df06de3deb59e7ea5fb78c9
BLAKE2b-256 checksum
How to use checksums
257fa72f5550b3520676e05c43f8da26b4264d994883e93c4c633f79b2f82a03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.5

Release history Release notifications | RSS feed

This release

0.16.0 This release

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.13.0

2 release files

0.12.6

2 release files

0.12.5

2 release files

0.12.4

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page