ExceptBot DRF
Exception Logger with AI Suggestions for Django REST Framework.
Overview
ExceptBot DRF is a Django middleware and REST API application that captures, logs, and helps resolve exceptions in Django/DRF projects. Superusers can view exceptions through REST endpoints, request AI-powered fix suggestions from OpenAI, and track resolution status.
This project is a fork of ExceptBot by Brian Risk, modified to support Django REST Framework APIs, custom user models, and enhanced request metadata.
Features
- Automatic exception capture via Django middleware
- Full stack trace with code snapshot of the offending file
- File name, line number, and exact line that raised the exception
- Request context: URL path, HTTP method, status code, IP address, user agent, and masked request body
- Source detection (
backend,frontend,unknown) via theX-Client-Typeheader - Smart deduplication: identical exceptions increment a
countfield - AI-powered fix suggestions via OpenAI ChatGPT
- Resolution tracking with
is_resolved,resolved_by,resolved_at, andresolution_note - REST API with superuser-only access
- Singleton
AppSettingsmodel - Works with any DRF authentication backend (JWT, Token, Session, OAuth)
- Custom user model support via
AUTH_USER_MODEL
Requirements
- Python >= 3.8
- Django >= 3.2
- djangorestframework >= 3.12
- openai >= 1.3.7
Installation
pip install exceptbot-drf
Setup
1. Add to INSTALLED_APPS
INSTALLED_APPS = [
# ...
'rest_framework',
'exceptbot',
# ...
]
2. Add the middleware
MIDDLEWARE = [
# ...
'django.contrib.auth.middleware.AuthenticationMiddleware',
# ...
'exceptbot.middleware.ExceptBotMiddleware',
]
AuthenticationMiddleware must appear before ExceptBotMiddleware so that request.user is available. If you have custom middleware that checks request.user, place ExceptBotMiddleware before it.
3. Configure DRF authentication
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
}
4. Include the URLs
from django.urls import path, include
urlpatterns = [
# ...
path('exceptbot/', include('exceptbot.urls', namespace='exceptbot')),
# ...
]
5. Run migrations
python manage.py migrate exceptbot
6. Configure settings
Navigate to /admin/exceptbot/appsettings/ and set the following fields:
| Field | Description |
|---|---|
project_name |
The directory name of your project (used to locate the correct file in the traceback) |
base_url |
Base URL of your site (e.g., https://myapp.com) |
openai_api_key |
OpenAI API key |
API Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /exceptbot/api/unresolved/ |
List unresolved exceptions |
| GET | /exceptbot/api/resolved/ |
List resolved exceptions |
| GET | /exceptbot/api/<id>/ |
Full exception detail |
| GET | /exceptbot/api/<id>/error/ |
Error message and traceback |
| GET | /exceptbot/api/<id>/file/ |
Content of the offending file |
| POST | /exceptbot/api/<id>/resolve/ |
Mark as resolved |
| POST | /exceptbot/api/<id>/unresolve/ |
Revert to unresolved |
| POST | /exceptbot/api/<id>/ai/ |
Get or generate AI suggestion |
| GET | /exceptbot/api/settings/ |
View settings |
| PUT | /exceptbot/api/settings/ |
Update settings |
All endpoints require superuser authentication.
Examples
Fetch unresolved exceptions:
curl -H "Authorization: Bearer YOUR_TOKEN" \
http://localhost:8000/exceptbot/api/unresolved/
Mark an exception as resolved:
curl -X POST \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"resolution_note": "Fixed in commit abc123"}' \
http://localhost:8000/exceptbot/api/1/resolve/
Request an AI suggestion:
curl -X POST \
-H "Authorization: Bearer YOUR_TOKEN" \
http://localhost:8000/exceptbot/api/1/ai/
Update settings:
curl -X PUT \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"openai_api_key": "sk-...",
"base_url": "https://myapp.com",
"project_name": "my_project"
}' \
http://localhost:8000/exceptbot/api/settings/
Source Detection
To distinguish frontend errors from backend errors, include the following header in requests:
X-Client-Type: frontend
If omitted, the default value is backend.
How It Works
- The middleware catches exceptions in
process_exceptionand inspects the traceback. - It walks the traceback in reverse to find the first frame inside the project, matched by
project_name. - It reads the offending file and stores the full source as a code snapshot.
- It extracts HTTP method, status code, IP address, user agent, and masked request body.
- Identical exceptions increment
countinstead of creating new records. - Superusers query the REST API to view, analyze, and resolve exceptions.
Security
- Sensitive request fields (
password,token,api_key,authorization, and similar) are replaced with***MASKED***before being stored. - All endpoints are protected by the
IsSuperUserpermission class. - The OpenAI API key is hidden in the admin list view and only shown as a boolean.
- Only one
AppSettingsrecord can exist.
Project Structure
exceptbot/
├── migrations/
├── admin.py
├── apps.py
├── middleware.py
├── models.py
├── permissions.py
├── serializers.py
├── urls.py
└── views.py
Contributing
Issues and pull requests are welcome at: https://github.com/tahazarei777/exceptbot-drf
Reporting Issues
Report bugs at: https://github.com/tahazarei777/exceptbot-drf/issues
License
This project is a fork of ExceptBot by Brian Risk.
Original work: Copyright © 2023-present, D.AT Analytics, LLC. All rights reserved.
Modifications: Copyright © 2025, Taha Zarei.
Licensed under the BSD 3-Clause License. See LICENSE.md for details.
Release files for exceptbot-drf 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| exceptbot_drf-1.0.1.tar.gz | 16.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| exceptbot_drf-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 33.0 kB
Release files / exceptbot_drf-1.0.1.tar.gz
| Download URL | exceptbot_drf-1.0.1.tar.gz |
|---|---|
| Size | 16.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
715c4ce192f2512d37570e9da001191af4cac3536d98b821098c4cd1f0ca44a4
|
|
BLAKE2b-256 checksum How to use checksums |
92214289618b864eaec91318e38db1e8c27eaf59f35423a5db251b7ffd66a987
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|
Release files / exceptbot_drf-1.0.1-py3-none-any.whl
| Download URL | exceptbot_drf-1.0.1-py3-none-any.whl |
|---|---|
| Size | 16.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6a962419824c7459278affb020cd0abb0846319d490425bce2fcf409d84b70de
|
|
BLAKE2b-256 checksum How to use checksums |
9687505c819733fa66f06cb4cb5c1cc3ddd9325981d938fa591a077871e7f504
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.10
|