Registry / web-framework / drf-exceptions-hog

drf-exceptions-hog

JSON →
library0.4.0pypypi✓ verified 28d ago

DRF Exceptions Hog provides standardized and easy-to-parse API error responses for Django REST Framework. It aims to unify the format of exceptions to simplify frontend parsing logic. The current version is 0.4.0, and it is actively maintained by PostHog with a moderate release cadence.

pip install drf-exceptions-hog
INSTALL
IMPORT
SIG · DRF-EXCEPTIONS-HOG
D
drf-exceptions-hog
web-frameworkpythonv0.4.0
Install
3.6s avg
Import
—
Disk
70MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 70.6MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.6s · import 0.000s · 71MB
70MB installed
● package 70MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

exception_handler
✓ REST_FRAMEWORK = { "EXCEPTION_HANDLER": "exceptions_hog.exception_handler" }
This is the primary way to integrate drf-exceptions-hog by configuring DRF's exception handler in your Django settings.

To integrate drf-exceptions-hog, update your Django REST Framework settings in your `settings.py` file to use `exceptions_hog.exception_handler` as the default exception handler. Optionally, you can configure `ENABLE_IN_DEBUG` to control its behavior in development mode.

# settings.py INSTALLED_APPS = [ # ... 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', # 'exceptions_hog' is not strictly required in INSTALLED_APPS # if only using the handler, but good practice if it has models/migrations. # For drf-exceptions-hog, it usually isn't needed. ] REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'exceptions_hog.exception_handler', # Optional: Enable in DEBUG mode to see standardized errors even during development. # By default, it's disabled when DEBUG=True to allow full stack traces. # 'DRF_EXCEPTIONS_HOG': {'ENABLE_IN_DEBUG': True} } # views.py (example usage for testing) from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework.exceptions import ValidationError class ExampleView(APIView): def get(self, request): if request.query_params.get('error'): raise ValidationError({'field': 'This is a validation error.'}) if request.query_params.get('server_error'): 1 / 0 # Simulate an unhandled server error return Response({"message": "Hello, world!"}, status=status.HTTP_200_OK)
Debug
Known issues
gotchaBy default, `drf-exceptions-hog` is disabled when `DEBUG=True` in Django settings. This is intentional to allow developers to see full stack traces for debugging. To enable standardized error responses even in debug mode, you must explicitly set `DRF_EXCEPTIONS_HOG = {"ENABLE_IN_DEBUG": True}` in your `settings.py`.
fix
To enable in debug mode, add `DRF_EXCEPTIONS_HOG = {'ENABLE_IN_DEBUG': True}` to your `settings.py`.
affects: All versions
gotchaVersions prior to `0.0.5` lacked explicit support for handling exceptions from deeply nested serializers, which could lead to inconsistent or incomplete error response formats for complex validation scenarios.
fix
Upgrade to version `0.0.5` or newer for improved handling of nested validation errors.
affects: <0.0.5
gotchaOlder versions might have encountered a `KeyError` when processing serializer validation errors that included the `__all__` key (used for non-field errors, e.g., from unique constraints or `full_clean` on a model). This issue was addressed in a fix related to GitHub issue #12.
fix
Ensure you are using a recent version of `drf-exceptions-hog` to avoid `KeyError` with `__all__` non-field errors.
affects: <0.0.4 (before fix for #12)
gotchaIn production (when `DEBUG=False`), `drf-exceptions-hog` intentionally returns a generic 'A server error occurred.' for unhandled 5xx exceptions. This is a security measure to prevent sensitive information leakage (e.g., raw tracebacks, internal paths). For detailed error reporting, integrate with an error monitoring tool like Sentry.
fix
Do not rely on `detail` field for unhandled 5xx errors in production. Use `ENABLE_IN_DEBUG: True` only in development, or configure a separate error monitoring service for production.
affects: All versions
Upgrade
Version history
0.4.0latest on PyPI · released May 8, 2023
Audit
Dependencies
djangorestframeworkrequiredThis library is an exception handler specifically for Django REST Framework.
djangorequiredAs a Django REST Framework utility, Django is a core dependency.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
Resources
drf-exceptions-hog — pip install drf-exceptions-hog · libregistry