Skip to main content

Django middleware that captures errors and exceptions, sends them to OpenAI for a detailed explanation, and prints the explanation to stdout when debug mode is enabled. Supports both sync and async views.

Project description

Django Explain Errors Middleware

This Django middleware captures errors and exceptions, sends them to OpenAI for explanation, and prints the explanation to stdout when debug mode is enabled. It uses an environment variable to securely manage the OpenAI API key.

The middleware supports both synchronous (WSGI) and asynchronous (ASGI) views. It auto-detects the view chain at startup and routes requests through the matching sync or async path, so no extra configuration is required to use it under either server type.

Features

  • Captures Django errors and exceptions
  • Uses OpenAI to explain the error
  • Securely manages the OpenAI API key using environment variables
  • Works with both sync (WSGI) and async (ASGI) views

Installation

  1. Install django-explain-errors by running:
pip install django-explain-errors
  1. Add the middleware to your Django project:

    • Open your settings.py file and add the middleware to the MIDDLEWARE list. Ensure that the middleware is added last in the list:

      MIDDLEWARE = [
          ...
          'explain_errors.middleware.ExplainErrorsMiddleware',
      ]
      
  2. Set up environment variables:

    • Create a .env file in your project's root directory and add your OpenAI API key. Alternatively, you can set the API key in settings.py:

      OPENAI_API_KEY=your_openai_api_key_here
      

Usage

  1. Ensure DEBUG is set to True:

    Open your settings.py file and set:

    DEBUG = True
    
  2. Trigger an error in your Django application:

    The middleware will capture the error, send it to OpenAI for explanation, and print the explanation to stdout. When an exception is caught, it returns a JSON 500 response containing the error message and the explanation.

Async Support

The middleware exposes both sync_capable = True and async_capable = True. At initialization it inspects get_response to decide whether it is part of a sync or async chain:

  • Under WSGI (for example runserver with sync views), requests flow through the synchronous handler.
  • Under ASGI (for example with async views), requests are awaited through the async handler. The blocking OpenAI call is offloaded with asgiref.sync.sync_to_async so the event loop is not blocked.

No additional settings are needed. Place the middleware last in MIDDLEWARE for both modes.

Configuration

Setting / variable Required Description
OPENAI_API_KEY (env or settings) Yes, when DEBUG=True API key used to authenticate with OpenAI. Read first from the environment, then from settings.
DEBUG Yes The middleware is only active when DEBUG=True. When False, requests pass through untouched.
OPENAI_MODEL No Model used for explanations. Defaults to gpt-4o-mini.
OPENAI_MAX_TOKENS No Maximum tokens in the explanation. Defaults to 150.
OPENAI_TIMEOUT No Request timeout in seconds for the OpenAI client. Defaults to 10.
OPENAI_MAX_TRACEBACK_CHARS No Traceback is trimmed to its last N characters before being sent. Defaults to 3000.

Example

Here is an example of how to use the middleware in a Django project:

# settings.py

DEBUG = True

MIDDLEWARE = [
    ...
    'explain_errors.middleware.ExplainErrorsMiddleware',
]

# .env

OPENAI_API_KEY=your_openai_api_key_here

When an error occurs, you will see an explanation printed to stdout.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

Acknowledgements

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

django_explain_errors-0.2.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_explain_errors-0.2.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file django_explain_errors-0.2.0.tar.gz.

File metadata

  • Download URL: django_explain_errors-0.2.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for django_explain_errors-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e8ac0f178be8571db1ac4dc321757fa671a93ea27e2cec01068d6e7d0c4fed57
MD5 29dc1692689556c9b75014c5ae57210c
BLAKE2b-256 ea7ceba12d3c762ba8ae633f669f2b6b072897e52b1f550fc88b55a5e23233c2

See more details on using hashes here.

File details

Details for the file django_explain_errors-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_explain_errors-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef547a6dd21c013def60c8eb77ec0c974b84dd2b17c9fb59cef98e7c8c892e25
MD5 f60aef69288d129508621ed6b89e8a7f
BLAKE2b-256 242e05c98933af33fd667c1dbe16aee8251979ef7bb71de1d79aec300c7fcdd6

See more details on using hashes here.

Supported by

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