Skip to main content

Common exception for Django REST framework

Project description

drf-common-exceptions

Release CI Coverage
pypi build codecov

Common exception for Django REST framework. Provides single generic interface of returning data structure for any kind of exceptions which are handled by Django REST framework. Includes error name, path to service with line where the error occurs and a list of actual error messages with extended fields info.

Requirements

  • Python (3.6+)
  • Django (1.11.x, 2.0+)
  • Django REST Framework (3.7+)

Installation

$ pip install drf-common-exceptions

Usage examples

You can define common exception handler for whole project. Just put the following line to your django settings inside drf section:

REST_FRAMEWORK = {
  ...
  "EXCEPTION_HANDLER": "drf_common_exceptions.common_exception_handler",
  ...
}

Or use it just for particular view or viewset:

from drf_common_exceptions import CommonExceptionHandlerMixin

class MyView(CommonExceptionHandlerMixin, APIView):
    pass

The output will looks like for example validation error:

{
    "service": "path.to.views.MyView:20",
    "error": "ValidationError",
    "detail": [
        {
            "label": "Name",
            "field": "name",
            "messages": [
                "This is required field."
            ]
        }
    ]
}

The data structure will be the same for any other errors.

Development

Install poetry and requirements:

$ curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
$ python3 -m venv path/to/venv
$ source path/to/venv/bin/activate
$ poetry install

Run main commands:

$ make test
$ make watch
$ make clean
$ make lint

Publish to pypi be default patch version:

$ make publish

or any level you want:

$ make publish minor

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-common-exceptions-0.1.1.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

drf_common_exceptions-0.1.1-py3-none-any.whl (8.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