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
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.
Features
- Captures Django errors and exceptions
- Uses OpenAI to explain the error
- Securely manages the OpenAI API key using environment variables
Installation
- Install django-explain-errors by running:
pip install django-explain-errors
-
Add the middleware to your Django project:
-
Open your
settings.py
file and add the middleware to theMIDDLEWARE
list. Ensure that the middleware is added last in the list:MIDDLEWARE = [ ... 'explain_errors.ExplainErrorsMiddleware', ]
-
-
Set up environment variables:
-
Create a
.env
file in your project’s root directory and add your OpenAI API key. Alternatively, you can list the API key insettings.py
:OPENAI_API_KEY=your_openai_api_key_here
-
Usage
-
Ensure DEBUG is set to True:
Open your
settings.py
file and set:DEBUG = True
-
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.
Example
Here is an example of how to use the middleware in a Django project:
# settings.py
DEBUG = True
MIDDLEWARE = [
...
'explain_errors.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file django_explain_errors-0.1-py3-none-any.whl
.
File metadata
- Download URL: django_explain_errors-0.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.0b3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | efac20b4052d0e5460f526291bb9203fbbefe86964437a921b70dde653f8a641 |
|
MD5 | 5522be9414cf96f01d70f848a56dbf08 |
|
BLAKE2b-256 | b4536a01f7917614b39fe6cc92d6cd0292f5728daac30a978e630bd81d7cd09d |