Skip to main content

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",
      "message": "This field is required.",
      "field": "name"
    },
    {
      "code": "max_length",
      "message": "Ensure this value has at most 100 characters.",
      "field": "title"
    }
  ]
}
{
  "type": "client_error",
  "errors": [
    {
      "code": "authentication_failed",
      "message": "Incorrect authentication credentials.",
      "field": null
    }
  ]
}
{
  "type": "server_error",
  "errors": [
    {
      "code": "error",
      "message": "A server error occurred.",
      "field": null
    }
  ]
}

Features

  • 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 elefanto-drf-exceptions

Add drf-standardized-errors to your installed apps

INSTALLED_APPS = [
    # other apps
    "elefanto_drf_exceptions",
]

Register the exception handler

REST_FRAMEWORK = {
    # other settings
    "EXCEPTION_HANDLER": "elefanto_drf_exceptions.handler.exception_handler"
}

Register the error formatter

DRF_STANDARDIZED_ERRORS = {
    "EXCEPTION_FORMATTER_CLASS": "elefanto_drf_exceptions.formatter.ElefantoExceptionFormatter",
}

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 = {
    # other settings
    "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

This project depends on drf-standardized-errors. For more information go to links below

Metadata

Release files for elefanto-drf-exceptions 0.1.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 elefanto-drf-exceptions 0.1.0
File Size Uploaded
elefanto_drf_exceptions-0.1.0.tar.gz 3.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elefanto-drf-exceptions 0.1.0
File Interpreter ABI Platform
elefanto_drf_exceptions-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / elefanto_drf_exceptions-0.1.0.tar.gz

Download URL elefanto_drf_exceptions-0.1.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aa4ff8ce01c684ef8e9098b046d0138232764b556b5e6b5ca4d267a145ac0591
BLAKE2b-256 checksum
How to use checksums
0b0fa0a67836fc667a14181b53f794a30db47cc9d6ef9d3d190fab92d60b330c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.6 Linux/5.19.0-41-generic

Release files / elefanto_drf_exceptions-0.1.0-py3-none-any.whl

Download URL elefanto_drf_exceptions-0.1.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78ca0324be0e80a6af5cf7ff74319e805d70fb3aa4851c7808b2b1cbff171738
BLAKE2b-256 checksum
How to use checksums
2338c00bec262be0865ee50b51fe2bd6b762e42b34c26773ac5aef0e0e395211
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.10.6 Linux/5.19.0-41-generic

Release history Release notifications | RSS feed

This release

0.1.0 This release

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