Skip to main content

Django Rest Framework pretty exception handler

Project description

Django Rest Framework pretty exception handler

https://pypi.python.org/pypi/drf_pretty_exception_handler https://travis-ci.com/ivlevdenis/drf_pretty_exception_handler https://drf-pretty-exception-handler.readthedocs.io/en/latest/?badge=latest https://pyup.io/repos/github/ivlevdenis/drf_pretty_exception_handler/

Django Rest Framework pretty exception handler

Dependencies

  • Python 3.7+
  • Django 2.0+
  • Django Rest Framework 3.10+

Setup

You can install the library directly from pypi using pip:

$ pip install drf-pretty-exception-handler

Edit your REST_FRAMEWORK settings in settings.py file:

REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'drf_pretty_exception_handler.exception_handler',
    ...
}

License

Free software: MIT license

Features

Default Django Rest Framework exception handler return errors in different formats.

Examples: Response on raise exceptions.APIException.

{
  "detail": "A server error occurred."
}

Response on raise exceptions.ValidationError.

[
  "Invalid input."
]

Response on raise exceptions.ValidationError if error in field serializator.

{
  "email": [
    "This field is required."
  ]
}

Response on raise exceptions.ValidationError in serializator .validate() .

{
  "non_field_errors": [
    "Passwords does not match"
  ]
}

This greatly complicates error handling in the frontend. This package provide own format of errors.

{
  "status_code": 500,
  "errors": {
    "non_field_errors": [
      "A server error occurred."
    ]
  }
}
{
  "status_code": 400,
  "errors": {
    "non_field_errors": [
      "Invalid input."
    ]
  }
}
{
  "status_code": 400,
  "errors": {
    "email": [
      "This field is required."
    ]
  }
}
{
  "status_code": 400,
  "errors": {
    "non_field_errors": [
      "Passwords does not match"
    ]
  }
}

And this package handle default Python exceptions.

l = [1, 2, 3, 4]
l[10]
{
  "status_code": 500,
  "errors": {
    "non_field_errors": [
      "IndexError: list index out of range"
    ]
  }
}

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Project details


Download files

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

Files for drf-pretty-exception-handler, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size drf_pretty_exception_handler-0.1.1-py2.py3-none-any.whl (4.6 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size drf_pretty_exception_handler-0.1.1.tar.gz (9.2 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page