Skip to main content

django-rest-framework exception handler build with functools.singledispatch

Project description

Django REST framework exception dispatcher

django-rest-framework exception handler build with funtools.singledispatch.

Installation

To use drf-exception-dispatcher simply install it with your package manager, e.g. to with pip:

pip install drf-exception-dispatcher

Then simply use exception_dispatcher.handlers.exception_handler ( or your own exception handler built on exception_dispatcher) in Django's settings REST_FRAMEWORK section:

REST_FRAMEWORK = {
  # ...
  'EXCEPTION_HANDLER': 'exception_dispatcher.handlers.exception_handler',
  # ...
}

Configuration

Following settings are present to make default exception_dispatcher handler configurable:

  • EXCEPTION_DISPATCHER_SET_ROLLBACK (defaults to True) - indicate if set_rollback should be called before returning response from exception handler
  • EXCEPTION_DISPATCHER_API_EXCEPTION_PARSER (defaults to exception_dispatcher.parsers.parse_rest_framework_api_exception') - import path to callable that is used to translate occurred exception to response data

Usage

To add new handlers to exception_dispatchers simply use dispatcher's register() method, e.g. to add handler of SuspiciousOperation exceptions:

from exception_dispatcher.dispatchers import exception_dispatcher
from exception_dispatcher.types import ContextType
from rest_framework.response import Response


@exception_dispatcher.register
def handler_suspicious_operation(
    exception: SuspiciousOperation,
    context: ContextType,
) -> Response | None:
    """Handle Django's `SuspiciousOperation` exceptions."""
    # custom ``exception` handler logic goes here
    return None

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-exception-dispatcher-0.1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file drf-exception-dispatcher-0.1.0.tar.gz.

File metadata

  • Download URL: drf-exception-dispatcher-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.10.0 Linux/5.11.0-1020-azure

File hashes

Hashes for drf-exception-dispatcher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c005798dd266716a1b21f2de6322cc1486a4e8b8f69ab4b7cb887b36a4afbe51
MD5 909cb37238504b134fc35d904da2f74c
BLAKE2b-256 297bffc9b69a2bab10cb4eebfba36b6723f9ba1ff46eef6d859c3a36ba2a97e3

See more details on using hashes here.

File details

Details for the file drf_exception_dispatcher-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_exception_dispatcher-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6064e938ee4f1c023db96d260624bd5f186a30fea5b3e17899b0dcac2f214568
MD5 2d0e638c0747c83d21ba4c46e4d25c6c
BLAKE2b-256 fcc49a80d1abf82595feadd5112158cb9edf72148ba4712a8b75d962a05a3d46

See more details on using hashes here.

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