Skip to main content

No project description provided

Project description

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

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

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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