{"id":4523,"library":"drf-exceptions-hog","title":"DRF Exceptions Hog","description":"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.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/posthog/drf-exceptions-hog","tags":["drf","django-rest-framework","exceptions","error-handling","api","standardization"],"install":[{"cmd":"pip install drf-exceptions-hog","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This library is an exception handler specifically for Django REST Framework.","package":"djangorestframework","optional":false},{"reason":"As a Django REST Framework utility, Django is a core dependency.","package":"django","optional":false}],"imports":[{"note":"This is the primary way to integrate drf-exceptions-hog by configuring DRF's exception handler in your Django settings.","symbol":"exception_handler","correct":"REST_FRAMEWORK = { \"EXCEPTION_HANDLER\": \"exceptions_hog.exception_handler\" }"}],"quickstart":{"code":"# settings.py\n\nINSTALLED_APPS = [\n    # ...\n    'django.contrib.admin',\n    'django.contrib.auth',\n    'django.contrib.contenttypes',\n    'django.contrib.sessions',\n    'django.contrib.messages',\n    'django.contrib.staticfiles',\n\n    'rest_framework',\n    # 'exceptions_hog' is not strictly required in INSTALLED_APPS\n    # if only using the handler, but good practice if it has models/migrations.\n    # For drf-exceptions-hog, it usually isn't needed.\n]\n\nREST_FRAMEWORK = {\n    'EXCEPTION_HANDLER': 'exceptions_hog.exception_handler',\n    # Optional: Enable in DEBUG mode to see standardized errors even during development.\n    # By default, it's disabled when DEBUG=True to allow full stack traces.\n    # 'DRF_EXCEPTIONS_HOG': {'ENABLE_IN_DEBUG': True}\n}\n\n# views.py (example usage for testing)\nfrom rest_framework.views import APIView\nfrom rest_framework.response import Response\nfrom rest_framework import status\nfrom rest_framework.exceptions import ValidationError\n\nclass ExampleView(APIView):\n    def get(self, request):\n        if request.query_params.get('error'):\n            raise ValidationError({'field': 'This is a validation error.'})\n        if request.query_params.get('server_error'):\n            1 / 0 # Simulate an unhandled server error\n        return Response({\"message\": \"Hello, world!\"}, status=status.HTTP_200_OK)","lang":"python","description":"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."},"warnings":[{"fix":"To enable in debug mode, add `DRF_EXCEPTIONS_HOG = {'ENABLE_IN_DEBUG': True}` to your `settings.py`.","message":"By 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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to version `0.0.5` or newer for improved handling of nested validation errors.","message":"Versions 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.","severity":"gotcha","affected_versions":"<0.0.5"},{"fix":"Ensure you are using a recent version of `drf-exceptions-hog` to avoid `KeyError` with `__all__` non-field errors.","message":"Older 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.","severity":"gotcha","affected_versions":"<0.0.4 (before fix for #12)"},{"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.","message":"In 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}