Skip to main content

Standardize your API error responses.

Project description

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 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

License

This project is MIT licensed.

Project details


Download files

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

Source Distribution

drf-standardized-errors-0.12.5.tar.gz (55.0 kB view details)

Uploaded Source

Built Distribution

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

drf_standardized_errors-0.12.5-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file drf-standardized-errors-0.12.5.tar.gz.

File metadata

File hashes

Hashes for drf-standardized-errors-0.12.5.tar.gz
Algorithm Hash digest
SHA256 20db40c4f4288c36046ee6e0e3727539fbe3306d60f6f1aa88326629022f69c4
MD5 90b0609f971ffeb7053e2f6114d7d7e6
BLAKE2b-256 b7a234c92496f0a0daba9d60211a864663e3c5e87a5eb868de22d64b0dcc1a13

See more details on using hashes here.

File details

Details for the file drf_standardized_errors-0.12.5-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_standardized_errors-0.12.5-py3-none-any.whl
Algorithm Hash digest
SHA256 46eda8c7442730a7d0069f6213ea588e5c386dc2858a14b9a3ae08e78fd8975d
MD5 122925014382887f9ec524447143eda3
BLAKE2b-256 d6461bd9b51cfa0b2aa01bea539c76dad066aeb60bdeaeb4234f398e5af0f640

See more details on using hashes here.

Supported by

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