Skip to main content

Never liked DRF plain old errors? Forget that and accept RFC 7807 - Problem Details!

Project description

DRF Problems PyPI version

TL;DR

This library implements RFC 7807 in our favorite Django REST Framework! Or, in layman terms, it introduces "Problem Details" in the HTTP APIs.

Features

  • Handles exception to return response with Problem Details model.
  • Added permission mixins and base class to store exception to raise by the view on failure of permission.
  • Added view mixin which throws exception of failed permission.
  • These permissions are compatible with composable permissions introduced in DRF 3.9.0!
  • Has problem description endpoint to understand about the problem with the given code.
  • Has sample permissions which checks for minimum API version.

Installation

Install the library as you would for any django library.

  • Install using pip. pip install drf-problems
  • Add 'drf_problems' to your INSTALLED_APPS setting.
INSTALLED_APPS = (
    ...
    'drf_problems',
)
  • DRF's default exception handler needs to be replaced. In your settings.py, update:
REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'drf_problems.exceptions.exception_handler',
  • To use the problem description url, you need to update your urls.py:
urlpatterns = [
    ...
    path('', include('drf_problems.urls'))
]

Usage

With exceptions

In your exception class, define default_code with the error code string which is used in the type URI. To set custom title, define title with the human-readable summary of the problem type. To set description, define description with a long paragraph describing the problem.

Finally, make sure to register your exception with drf_problems.utils.register_exception function. Here's a sample exception class:

from drf_problems.utils import register_exception

class InvalidVersionRequestedException(exceptions.NotAcceptable):
    default_code = 'invalid_version'
    title = 'Invalid API version'
    default_detail = 'Provided API version is invalid.')
    description = 'Malformed or unsupported version string is provided with the request.'

register_exception(InvalidVersionRequestedException)

With permissions

Use either drf_problems.permissions.ProblemPermissionMixin mixin class with your existing permissions, or extend directly from drf_problems.permissions.BaseProblemPermission. Define exception_class in the permissions to the desired exception class. For flexibility, you can even set exception instance by setting exception attribute on the permission object.

Here's a sample permissions class:

from drf_problems.permissions import BaseProblemPermission

class MinimumVersionRequiredPermission(BaseProblemPermission):
    exception_class = InvalidVersionRequestedException

With Views

Note: The permissions wouldn't throw the desired exception from the view, until the view is extended from the drf_problems.mixins.AllowPermissionWithExceptionViewMixin mixin. So, remember to update your views too, for which permissions are updated!

Resources

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-problems-0.9.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

drf_problems-0.9.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file drf-problems-0.9.2.tar.gz.

File metadata

  • Download URL: drf-problems-0.9.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for drf-problems-0.9.2.tar.gz
Algorithm Hash digest
SHA256 8902b18081e934b62e2a18f7da89512b1692d14f07e4e2d3bb7dae1270ba7350
MD5 d2aa8865328b104a3070540472f6d133
BLAKE2b-256 c05c71f51a1a1ccd76778f38b90ff10f77b5f4b38db453d31abc4a68812946b4

See more details on using hashes here.

File details

Details for the file drf_problems-0.9.2-py3-none-any.whl.

File metadata

  • Download URL: drf_problems-0.9.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for drf_problems-0.9.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fa70cb11f838bfe4f9f27c4ba4c97cc4725c46e37d18e6ca4412a5978602f401
MD5 7ae60a96e043628bccf8b5bc824fbd00
BLAKE2b-256 79b08275dd4fb25a738f63c373e0eab553954ae606d13d7e73221cf199e966ab

See more details on using hashes here.

Supported by

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