django-explain-errors 0.4.0__tar.gz → 0.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. django_explain_errors-0.5.0/PKG-INFO +383 -0
  2. django_explain_errors-0.5.0/README.md +333 -0
  3. django_explain_errors-0.5.0/django_explain_errors.egg-info/PKG-INFO +383 -0
  4. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/django_explain_errors.egg-info/SOURCES.txt +3 -1
  5. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/middleware.py +20 -3
  6. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/setup.py +4 -1
  7. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_middleware.py +53 -5
  8. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_rag.py +4 -0
  9. django_explain_errors-0.5.0/tests/test_signals.py +55 -0
  10. django_explain_errors-0.5.0/tests/test_truncation.py +95 -0
  11. django_explain_errors-0.4.0/PKG-INFO +0 -267
  12. django_explain_errors-0.4.0/README.md +0 -220
  13. django_explain_errors-0.4.0/django_explain_errors.egg-info/PKG-INFO +0 -267
  14. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/LICENSE +0 -0
  15. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/MANIFEST.in +0 -0
  16. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/django_explain_errors.egg-info/dependency_links.txt +0 -0
  17. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/django_explain_errors.egg-info/requires.txt +0 -0
  18. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/django_explain_errors.egg-info/top_level.txt +0 -0
  19. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/__init__.py +0 -0
  20. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/client.py +0 -0
  21. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/management/__init__.py +0 -0
  22. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/management/commands/__init__.py +0 -0
  23. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/management/commands/build_error_index.py +0 -0
  24. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/rag/__init__.py +0 -0
  25. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/rag/indexer.py +0 -0
  26. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/rag/retriever.py +0 -0
  27. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/rag/store.py +0 -0
  28. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/sanitize.py +0 -0
  29. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/explain_errors/throttle.py +0 -0
  30. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/setup.cfg +0 -0
  31. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_build_error_index.py +0 -0
  32. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_client.py +0 -0
  33. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_sanitize.py +0 -0
  34. {django_explain_errors-0.4.0 → django_explain_errors-0.5.0}/tests/test_throttle.py +0 -0
@@ -0,0 +1,383 @@
1
+ Metadata-Version: 2.4
2
+ Name: django-explain-errors
3
+ Version: 0.5.0
4
+ Summary: 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.
5
+ Home-page: https://github.com/topunix/django-explain-errors
6
+ Author: topunix
7
+ Author-email: topunixguy@gmail.com
8
+ License: MIT
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Environment :: Web Environment
11
+ Classifier: Framework :: Django
12
+ Classifier: Framework :: Django :: 4.2
13
+ Classifier: Framework :: Django :: 5.0
14
+ Classifier: Framework :: Django :: 5.1
15
+ Classifier: Framework :: Django :: 5.2
16
+ Classifier: Framework :: Django :: 6.0
17
+ Classifier: Framework :: Django :: 6.1
18
+ Classifier: Intended Audience :: Developers
19
+ Classifier: License :: OSI Approved :: MIT License
20
+ Classifier: Operating System :: OS Independent
21
+ Classifier: Programming Language :: Python
22
+ Classifier: Programming Language :: Python :: 3
23
+ Classifier: Programming Language :: Python :: 3.9
24
+ Classifier: Programming Language :: Python :: 3.10
25
+ Classifier: Programming Language :: Python :: 3.11
26
+ Classifier: Programming Language :: Python :: 3.12
27
+ Classifier: Programming Language :: Python :: 3.13
28
+ Classifier: Topic :: Internet :: WWW/HTTP
29
+ Requires-Python: >=3.9
30
+ Description-Content-Type: text/markdown
31
+ License-File: LICENSE
32
+ Requires-Dist: Django>=4.2
33
+ Requires-Dist: openai<4.0,>=1.0
34
+ Requires-Dist: python-dotenv>=1.0
35
+ Requires-Dist: asgiref>=3.6
36
+ Provides-Extra: rag
37
+ Requires-Dist: sqlite-vec>=0.1.0; extra == "rag"
38
+ Dynamic: author
39
+ Dynamic: author-email
40
+ Dynamic: classifier
41
+ Dynamic: description
42
+ Dynamic: description-content-type
43
+ Dynamic: home-page
44
+ Dynamic: license
45
+ Dynamic: license-file
46
+ Dynamic: provides-extra
47
+ Dynamic: requires-dist
48
+ Dynamic: requires-python
49
+ Dynamic: summary
50
+
51
+ # Django Explain Errors Middleware
52
+
53
+ This Django middleware captures unhandled errors and exceptions, sends them
54
+ to a language model for explanation, and prints the explanation to stdout
55
+ when debug mode is enabled. It works with the OpenAI API out of the box, with
56
+ Anthropic's Claude models through Anthropic's OpenAI-compatible endpoint, and
57
+ with any other OpenAI-compatible endpoint (Ollama, LM Studio, Azure, or a
58
+ corporate gateway) by setting a base URL, so explanations can run entirely on
59
+ a local model if you prefer not to send code off your machine.
60
+
61
+ It can optionally ground explanations in your own project source using a
62
+ local vector index (RAG), so explanations reference the actual code that
63
+ failed instead of staying generic.
64
+
65
+ The middleware supports both synchronous (WSGI) and asynchronous (ASGI)
66
+ views. It auto-detects the view chain at startup and routes requests through
67
+ the matching path, so no extra configuration is required for either server
68
+ type. Tracebacks are sanitized before leaving the process, and API calls are
69
+ rate limited.
70
+
71
+ ## Scope
72
+
73
+ This package explains errors for a person, not for a coding agent to consume
74
+ programmatically. The explanation is written for a human reader, in a terminal or an editor's
75
+ integrated terminal, and the output format assumes that reader.
76
+
77
+ If a coding agent is doing the debugging, it does not need this. Agents read tracebacks directly,
78
+ and tools that expose live runtime state (debugger-over-MCP servers, `mcp-django`) serve that case
79
+ better. This package is not trying to compete there.
80
+
81
+ Local development only. It requires `DEBUG = True` and is inert otherwise.
82
+
83
+ ## Features
84
+
85
+ - Captures Django errors and exceptions
86
+ - Explains errors using OpenAI, Anthropic's Claude models, or any other
87
+ OpenAI-compatible endpoint (Ollama, LM Studio, Azure, gateways) via `OPENAI_BASE_URL`
88
+ - Optional codebase-aware explanations (RAG) backed by a local sqlite-vec index (see the RAG section below)
89
+ - Redacts secrets, tokens, and emails from tracebacks before sending
90
+ - Rate limits API calls with a configurable sliding window
91
+ - Works with both sync (WSGI) and async (ASGI) views
92
+ - Manages the API key using environment variables
93
+
94
+ ## Installation
95
+
96
+ 1. Install django-explain-errors by running:
97
+ ```bash
98
+ pip install django-explain-errors
99
+ ```
100
+
101
+ 2. **Add the middleware to your Django project**:
102
+
103
+ - Open your `settings.py` file and add the middleware to the `MIDDLEWARE` list:
104
+
105
+ ```python
106
+ MIDDLEWARE = [
107
+ ...
108
+ 'explain_errors.middleware.ExplainErrorsMiddleware',
109
+ ]
110
+ ```
111
+
112
+ In the default preserve mode, `process_exception` returns `None`, so exception handling
113
+ continues normally no matter where the middleware sits in the list — it no longer needs
114
+ to be last to avoid pre-empting other packages' error handling. It still needs to sit
115
+ close enough to the view that unhandled exceptions actually reach it, before any other
116
+ middleware that might catch and handle them itself. If you set
117
+ `EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE=False`, keep it last: returning a response there still
118
+ ends exception handling early and pre-empts anything above it in the stack (Debug Toolbar,
119
+ Sentry, Rollbar — see Compatibility below).
120
+
121
+ 3. **Set up environment variables**:
122
+
123
+ - 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`:
124
+
125
+ ```plaintext
126
+ OPENAI_API_KEY=your_openai_api_key_here
127
+ ```
128
+
129
+ The API key is not required if you set `OPENAI_BASE_URL` to a local
130
+ server such as Ollama, which does not authenticate requests.
131
+
132
+ ## Usage
133
+
134
+ 1. **Ensure DEBUG is set to True**:
135
+
136
+ Open your `settings.py` file and set:
137
+
138
+ ```python
139
+ DEBUG = True
140
+ ```
141
+
142
+ 2. **Trigger an error in your Django application**:
143
+
144
+ The middleware captures the error, sends it to the configured model for explanation, and prints the explanation to stdout. By default (`EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE=True`), it then lets exception handling continue normally, so Django (or whatever else is watching, such as `runserver_plus` or Sentry — see Compatibility below) renders exactly what it would without this middleware installed. Set `EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE = False` to instead get a JSON `500` response containing the error message and the explanation.
145
+
146
+ ## Async Support
147
+
148
+ 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:
149
+
150
+ - Under WSGI (for example `runserver` with sync views), requests flow through the synchronous handler.
151
+ - 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.
152
+
153
+ No additional settings are needed. See Installation above for where to place the middleware in `MIDDLEWARE`.
154
+
155
+ ## Compatibility
156
+
157
+ How this middleware interacts with other error-handling and debugging tools, in the default
158
+ preserve mode and with `EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE=False`:
159
+
160
+ | Package | Preserve mode (default) | `EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE=False` |
161
+ | ------- | ------------------------ | ------------------------------------------- |
162
+ | Django Debug Toolbar | Works — Django renders its normal debug page, and Debug Toolbar injects into it. | Broken — the JSON 500 response has no HTML to inject into. |
163
+ | Sentry, Rollbar | Work — the exception propagates and Django re-raises it, so `got_request_exception` fires. | Broken — returning a response ends exception handling before `got_request_exception` fires. |
164
+ | Django REST Framework | Partial — only exceptions DRF does not already handle itself reach this middleware. | Partial, same reason. |
165
+ | Silk and other profiling panels | Timings are inflated by the OpenAI call, since `process_exception` blocks the request path. | Same. |
166
+ | CORS, GZip, WhiteNoise | No interaction. | No interaction. |
167
+ | `runserver_plus` / Werkzeug debugger | Works — returning `None` re-raises the original exception, and `django-extensions` replaces Django's debug-page renderer with one that re-raises instead, so Werkzeug's WSGI wrapper catches it and shows the interactive debugger. | Broken — the JSON 500 response ends exception handling before it reaches `runserver_plus`'s exception hook, so the Werkzeug debugger never appears. |
168
+
169
+ Debug Toolbar, Sentry, and Werkzeug were verified empirically, on both Django 4.2 and Django
170
+ 6.1, in both modes. DRF, Silk, and CORS/GZip/WhiteNoise are reasoned from the mechanism
171
+ rather than tested.
172
+
173
+ One additional behavior worth knowing, also verified on both Django versions: when the
174
+ explanation call itself fails (a bad key, a timeout, an unreachable endpoint), a
175
+ Sentry-instrumented project captures a separate event for that failure. `explain_errors`
176
+ catches the exception internally, so it never becomes an unhandled exception, but Sentry's
177
+ `httpx` integration captures it anyway by instrumenting inside the HTTP client library rather
178
+ than relying on `got_request_exception`. That event is unrelated to whatever error the
179
+ developer is actually investigating.
180
+
181
+ ## Configuration
182
+
183
+ | Setting / variable | Required | Description |
184
+ | ------------------ | -------- | ----------- |
185
+ | `OPENAI_API_KEY` (env or settings) | Yes, unless `OPENAI_BASE_URL` points at an endpoint that does not authenticate | API key used to authenticate with the configured endpoint. Read first from the environment, then from `settings`. |
186
+ | `DEBUG` | Yes | The middleware is only active when `DEBUG=True`. When `False`, requests pass through untouched. |
187
+ | `OPENAI_MODEL` | No | Model used for explanations. Defaults to `gpt-4o-mini`. |
188
+ | `OPENAI_MAX_TOKENS` | No | Ceiling on tokens generated for the explanation, not a target — the system prompt itself asks for a concise answer. Defaults to `1000`. |
189
+ | `OPENAI_TIMEOUT` | No | Request timeout in seconds for the OpenAI client. Defaults to `10`. |
190
+ | `OPENAI_MAX_TRACEBACK_CHARS` | No | Traceback is trimmed to its last N characters before being sent. Defaults to `3000`. |
191
+ | `EXPLAIN_ERRORS_PRESERVE_DEBUG_PAGE` | No | When `True` (the default), the middleware prints the explanation to stdout and returns `None`, so exception handling continues normally and Django renders its standard debug page. Set to `False` to instead return a JSON 500 response, which ends exception handling early (see Compatibility above). |
192
+ | `OPENAI_BASE_URL` (env or settings) | No | Base URL for any OpenAI-compatible API (for example Ollama at `http://localhost:11434/v1`). When set, a missing API key is replaced with a placeholder since local servers do not require one. |
193
+ | `EXPLAIN_ERRORS_MAX_CALLS` | No | Together with `EXPLAIN_ERRORS_WINDOW_SECONDS`, caps API spend to at most this many explanations within a rolling window; once the cap is hit, further errors in that window are not sent for explanation until an earlier call ages out. Defaults to `5`. |
194
+ | `EXPLAIN_ERRORS_WINDOW_SECONDS` | No | Length in seconds of the rolling window `EXPLAIN_ERRORS_MAX_CALLS` is measured against. Defaults to `60` (with the defaults, at most 5 explanations per 60-second window). |
195
+ | `EXPLAIN_ERRORS_REDACT_PATTERNS` | No | Extra regex pattern strings (each passed to `re.compile`) applied to the traceback, appended after the built-in secret/token/email patterns. An invalid pattern is skipped with a warning rather than raising. Defaults to `[]`. |
196
+ | `EXPLAIN_ERRORS_REDACT_DISABLE_DEFAULTS` | No | When `True`, skips the built-in secret/token/email redaction patterns entirely and redacts only what `EXPLAIN_ERRORS_REDACT_PATTERNS` specifies. Turning this on removes the default protection against leaking secrets and PII in tracebacks. Defaults to `False`. |
197
+ | `EXPLAIN_ERRORS_REDACT_REPLACEMENT` | No | Replacement string substituted for anything matched by the redaction patterns. Defaults to `"[REDACTED]"`. |
198
+
199
+ ## Using local models (Ollama)
200
+
201
+ Point `OPENAI_BASE_URL` at any OpenAI-compatible server to run explanations
202
+ against a local model instead of the OpenAI API:
203
+
204
+ ```python
205
+ OPENAI_BASE_URL = "http://localhost:11434/v1"
206
+ OPENAI_MODEL = "llama3.1"
207
+ EXPLAIN_ERRORS_RAG_EMBED_MODEL = "nomic-embed-text"
208
+ ```
209
+
210
+ With a local endpoint, no traceback or source code leaves your machine,
211
+ which matters if you work somewhere that cannot send code to a
212
+ third-party API.
213
+
214
+ If you use the RAG layer, rebuild the index after changing the embedding
215
+ model or provider. Stored vectors are model-specific.
216
+
217
+ ## Using Anthropic (Claude) models
218
+
219
+ Anthropic's Claude models work today through Anthropic's OpenAI-compatible API, with no
220
+ Anthropic-specific code required:
221
+
222
+ ```python
223
+ OPENAI_BASE_URL = "https://api.anthropic.com/v1/"
224
+ OPENAI_API_KEY = "your_anthropic_api_key_here"
225
+ OPENAI_MODEL = "..." # see Anthropic's current model list below
226
+ ```
227
+
228
+ `OPENAI_MODEL` is mandatory here: the default (`gpt-4o-mini`) doesn't exist on Anthropic's API
229
+ and will 404. Use one of the model names from
230
+ [Anthropic's model overview](https://platform.claude.com/docs/en/about-claude/models/overview);
231
+ model names are versioned and retired over time, so check that page rather than relying on a
232
+ name pinned here.
233
+
234
+ Anthropic documents this compatibility layer as intended primarily for testing and comparing
235
+ model capabilities, not as a production integration path. That's an acceptable tradeoff for a
236
+ development-only middleware, but worth knowing going in.
237
+
238
+ Reasoning models behind `OPENAI_BASE_URL` spend part of the token budget on internal reasoning
239
+ before producing visible output. At a low `OPENAI_MAX_TOKENS`, the budget can be used up by
240
+ reasoning alone, and the explanation comes back empty. Raise `OPENAI_MAX_TOKENS` if you see this.
241
+
242
+ ### A note on API keys and 401s
243
+
244
+ `explain_errors` reads `OPENAI_API_KEY` (see Configuration above); it does not read
245
+ provider-specific variables such as `ANTHROPIC_API_KEY`. If no key is found and
246
+ `OPENAI_BASE_URL` is set, the client substitutes a placeholder key rather than raising an
247
+ error — a convenience for local servers like Ollama or LM Studio, which ignore the key
248
+ entirely. Against a real remote endpoint such as Anthropic's, that placeholder is sent as-is
249
+ and rejected, so a missing `OPENAI_API_KEY` shows up as an opaque `401 Unauthorized` rather
250
+ than a clear configuration error. If you see a 401 with `OPENAI_BASE_URL` pointed at a remote
251
+ provider, check that `OPENAI_API_KEY` — not a provider-specific variable — is actually set.
252
+
253
+ ## Codebase-aware explanations (RAG)
254
+
255
+ By default, explanations are generated from the traceback alone. With the
256
+ optional RAG (retrieval-augmented generation) layer enabled, the middleware
257
+ also retrieves the most relevant chunks of your own project's source code
258
+ from a local vector index and includes them in the prompt, so explanations
259
+ can reference your actual functions and classes instead of guessing at them.
260
+
261
+ This feature is opt-in and adds no dependencies or behavior unless enabled.
262
+
263
+ ### Install the extra
264
+
265
+ ```bash
266
+ pip install django-explain-errors[rag]
267
+ ```
268
+
269
+ This pulls in [sqlite-vec](https://github.com/asg017/sqlite-vec), a
270
+ single-file, no-server vector store. The core package stays dependency-light
271
+ if you don't need RAG.
272
+
273
+ ### Build the index
274
+
275
+ Add `explain_errors` to `INSTALLED_APPS` (needed for Django to discover the
276
+ management command), then run:
277
+
278
+ ```bash
279
+ python manage.py build_error_index
280
+ ```
281
+
282
+ This walks your project, chunks Python files by top-level function/class
283
+ (and other text files by fixed-size line windows), embeds each chunk with
284
+ the OpenAI embeddings API, and writes them to a local index file. Re-run it
285
+ whenever your source changes meaningfully. Indexing is not automatic.
286
+ Rebuilding is idempotent: it builds into a temp file and atomically replaces
287
+ the previous index.
288
+
289
+ ### Enable it
290
+
291
+ ```python
292
+ # settings.py
293
+
294
+ EXPLAIN_ERRORS_RAG_ENABLED = True
295
+ ```
296
+
297
+ ### Settings
298
+
299
+ | Setting | Default | Description |
300
+ | ------- | ------- | ----------- |
301
+ | `EXPLAIN_ERRORS_RAG_ENABLED` | `False` | Master switch for the RAG layer. |
302
+ | `EXPLAIN_ERRORS_RAG_INDEX_PATH` | `<BASE_DIR>/.explain_errors_index.db` | Path to the local vector index file. |
303
+ | `EXPLAIN_ERRORS_RAG_TOP_K` | `4` | Number of chunks retrieved and injected into the prompt. |
304
+ | `EXPLAIN_ERRORS_RAG_EMBED_MODEL` | `"text-embedding-3-small"` | OpenAI embedding model used for indexing and retrieval. |
305
+ | `EXPLAIN_ERRORS_RAG_INCLUDE` | `None` (defaults to `BASE_DIR`) | List of directories to index. |
306
+ | `EXPLAIN_ERRORS_RAG_EXCLUDE` | migrations, venvs, `node_modules`, static, media, `.git` | Directory names to skip while indexing. |
307
+ | `EXPLAIN_ERRORS_RAG_MAX_PROMPT_CHARS` | `6000` | Combined character budget for the traceback + retrieved source sections of the prompt. |
308
+
309
+ Every chunk of source code and every retrieval query is passed through the
310
+ same `sanitize_traceback()` redaction used for tracebacks, which strips
311
+ patterns that look like secrets, tokens, and emails before anything is sent
312
+ to OpenAI or written to the index. It's a pattern-based filter, not a
313
+ guarantee: it catches recognizable secret shapes, not arbitrary sensitive
314
+ data that doesn't match one.
315
+
316
+ If RAG is enabled but the index is missing, `sqlite-vec` isn't installed, or
317
+ retrieval fails for any reason, the middleware logs a warning and falls back
318
+ to the traceback-only prompt. It never breaks error reporting.
319
+
320
+ RAG-grounded explanations tend to be longer than traceback-only ones. The default `OPENAI_MAX_TOKENS` already leaves generous headroom for this, but if you've lowered it, raise it back up when RAG is enabled so explanations are not truncated.
321
+
322
+ ### .gitignore
323
+
324
+ The index file is a local build artifact, not something to commit. Add it
325
+ to your project's `.gitignore`:
326
+
327
+ ```
328
+ .explain_errors_index.db
329
+ ```
330
+
331
+ (Adjust the path if you set `EXPLAIN_ERRORS_RAG_INDEX_PATH` to something
332
+ else.)
333
+
334
+ ### Before / after
335
+
336
+ **Without RAG**, traceback only:
337
+
338
+ > Your `ValueError` is raised because the value passed to `foo()` couldn't
339
+ > be converted to an integer. Check where `foo()` is called and make sure
340
+ > you're passing a numeric string.
341
+
342
+ **With RAG**, grounded in the actual function:
343
+
344
+ > In `myapp/utils.py`, `foo()` calls `int(value)` on line 12 without a
345
+ > `try`/`except`, so any non-numeric `value` raises `ValueError` straight
346
+ > through to the caller. Since `foo()` is called from `myapp/views.py` with
347
+ > unvalidated form input, add validation there or wrap the `int()` call in
348
+ > `foo()` with a clear error message.
349
+
350
+ ## Example
351
+
352
+ Here is an example of how to use the middleware in a Django project:
353
+
354
+ ```python
355
+ # settings.py
356
+
357
+ DEBUG = True
358
+
359
+ MIDDLEWARE = [
360
+ ...
361
+ 'explain_errors.middleware.ExplainErrorsMiddleware',
362
+ ]
363
+
364
+ # .env
365
+
366
+ OPENAI_API_KEY=your_openai_api_key_here
367
+ ```
368
+
369
+ When an error occurs, you will see an explanation printed to stdout.
370
+
371
+ ## License
372
+
373
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
374
+
375
+ ## Contributing
376
+
377
+ Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
378
+
379
+ ## Acknowledgements
380
+
381
+ - [Django](https://www.djangoproject.com/)
382
+ - [OpenAI](https://www.openai.com/)
383
+ - [python-dotenv](https://github.com/theskumar/python-dotenv)