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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elefanto_drf_exceptions-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

elefanto_drf_exceptions-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file elefanto_drf_exceptions-0.1.0.tar.gz.

File metadata

  • Download URL: elefanto_drf_exceptions-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.10.6 Linux/5.19.0-41-generic

File hashes

Hashes for elefanto_drf_exceptions-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa4ff8ce01c684ef8e9098b046d0138232764b556b5e6b5ca4d267a145ac0591
MD5 793f48bb4e573e2d1db3a0b97d86d80f
BLAKE2b-256 0b0fa0a67836fc667a14181b53f794a30db47cc9d6ef9d3d190fab92d60b330c

See more details on using hashes here.

File details

Details for the file elefanto_drf_exceptions-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for elefanto_drf_exceptions-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78ca0324be0e80a6af5cf7ff74319e805d70fb3aa4851c7808b2b1cbff171738
MD5 d02ca44c77223bd3cc53ff691477be54
BLAKE2b-256 2338c00bec262be0865ee50b51fe2bd6b762e42b34c26773ac5aef0e0e395211

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page