exceptbot-drf 1.0.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.
- exceptbot_drf-1.0.0/LICENSE.md +49 -0
- exceptbot_drf-1.0.0/MANIFEST.in +3 -0
- exceptbot_drf-1.0.0/PKG-INFO +277 -0
- exceptbot_drf-1.0.0/README.md +234 -0
- exceptbot_drf-1.0.0/exceptbot/__init__.py +1 -0
- exceptbot_drf-1.0.0/exceptbot/admin.py +260 -0
- exceptbot_drf-1.0.0/exceptbot/apps.py +7 -0
- exceptbot_drf-1.0.0/exceptbot/middleware.py +126 -0
- exceptbot_drf-1.0.0/exceptbot/migrations/0001_initial.py +63 -0
- exceptbot_drf-1.0.0/exceptbot/migrations/__init__.py +0 -0
- exceptbot_drf-1.0.0/exceptbot/models.py +134 -0
- exceptbot_drf-1.0.0/exceptbot/permissions.py +11 -0
- exceptbot_drf-1.0.0/exceptbot/serializers.py +65 -0
- exceptbot_drf-1.0.0/exceptbot/tests.py +0 -0
- exceptbot_drf-1.0.0/exceptbot/urls.py +85 -0
- exceptbot_drf-1.0.0/exceptbot/views.py +272 -0
- exceptbot_drf-1.0.0/exceptbot_drf.egg-info/PKG-INFO +277 -0
- exceptbot_drf-1.0.0/exceptbot_drf.egg-info/SOURCES.txt +21 -0
- exceptbot_drf-1.0.0/exceptbot_drf.egg-info/dependency_links.txt +1 -0
- exceptbot_drf-1.0.0/exceptbot_drf.egg-info/requires.txt +9 -0
- exceptbot_drf-1.0.0/exceptbot_drf.egg-info/top_level.txt +1 -0
- exceptbot_drf-1.0.0/pyproject.toml +78 -0
- exceptbot_drf-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# License
|
|
2
|
+
|
|
3
|
+
Copyright © 2023-present, [D.AT Analytics, LLC](https://d.at/).
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
* Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
21
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
22
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Modifications
|
|
34
|
+
|
|
35
|
+
This project is a fork of [ExceptBot](https://github.com/geneffects/exceptbot),
|
|
36
|
+
originally created by Brian Risk and D.AT Analytics, LLC.
|
|
37
|
+
|
|
38
|
+
Modifications Copyright © 2025, Taha Zarei.
|
|
39
|
+
|
|
40
|
+
The following changes were made to the original work:
|
|
41
|
+
|
|
42
|
+
- Converted HTML views to Django REST Framework API endpoints
|
|
43
|
+
- Replaced `auth.User` with `settings.AUTH_USER_MODEL` for custom user model support
|
|
44
|
+
- Added new model fields: `http_method`, `status_code`, `source`, `ip_address`, `user_agent`, `request_data`, `resolution_note`
|
|
45
|
+
- Added source detection via the `X-Client-Type` header
|
|
46
|
+
- Added sensitive data masking for request bodies
|
|
47
|
+
- Added singleton pattern for `AppSettings` with `get_solo()` method
|
|
48
|
+
- Added `mark_resolved()` method to `ExceptionLog`
|
|
49
|
+
- Removed form-based UI and HTML templates
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: exceptbot-drf
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Exception Logger with AI Suggestions for Django REST Framework
|
|
5
|
+
Author-email: Taha Zarei <tahazarei777@gmail.com>
|
|
6
|
+
Maintainer-email: Taha Zarei <tahazarei777@gmail.com>
|
|
7
|
+
License-Expression: BSD-3-Clause
|
|
8
|
+
Project-URL: Homepage, https://github.com/tahazarei777/exceptbot-drf
|
|
9
|
+
Project-URL: Repository, https://github.com/tahazarei777/exceptbot-drf.git
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/tahazarei777/exceptbot-drf/issues
|
|
11
|
+
Project-URL: Changelog, https://github.com/tahazarei777/exceptbot-drf/blob/main/CHANGELOG.md
|
|
12
|
+
Keywords: django,django-rest-framework,drf,exception,logger,error-tracking,middleware,openai,chatgpt,api
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: Django
|
|
15
|
+
Classifier: Framework :: Django :: 3.2
|
|
16
|
+
Classifier: Framework :: Django :: 4.0
|
|
17
|
+
Classifier: Framework :: Django :: 4.1
|
|
18
|
+
Classifier: Framework :: Django :: 4.2
|
|
19
|
+
Classifier: Framework :: Django :: 5.0
|
|
20
|
+
Classifier: Intended Audience :: Developers
|
|
21
|
+
Classifier: Operating System :: OS Independent
|
|
22
|
+
Classifier: Programming Language :: Python :: 3
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
30
|
+
Classifier: Topic :: System :: Logging
|
|
31
|
+
Requires-Python: >=3.8
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
License-File: LICENSE.md
|
|
34
|
+
Requires-Dist: Django>=3.2
|
|
35
|
+
Requires-Dist: djangorestframework>=3.12
|
|
36
|
+
Requires-Dist: openai>=1.3.7
|
|
37
|
+
Provides-Extra: dev
|
|
38
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
39
|
+
Requires-Dist: twine>=4.0.0; extra == "dev"
|
|
40
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
41
|
+
Requires-Dist: pytest-django>=4.5.0; extra == "dev"
|
|
42
|
+
Dynamic: license-file
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
# ExceptBot DRF
|
|
46
|
+
|
|
47
|
+
Exception Logger with AI Suggestions for Django REST Framework.
|
|
48
|
+
|
|
49
|
+
[](https://pypi.org/project/exceptbot-drf/)
|
|
50
|
+
[](https://www.python.org/)
|
|
51
|
+
[](https://www.djangoproject.com/)
|
|
52
|
+
[](https://www.django-rest-framework.org/)
|
|
53
|
+
[](LICENSE.md)
|
|
54
|
+
|
|
55
|
+
## Overview
|
|
56
|
+
|
|
57
|
+
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.
|
|
58
|
+
|
|
59
|
+
This project is a fork of [ExceptBot](https://github.com/geneffects/exceptbot) by Brian Risk, modified to support Django REST Framework APIs, custom user models, and enhanced request metadata.
|
|
60
|
+
|
|
61
|
+
## Features
|
|
62
|
+
|
|
63
|
+
- Automatic exception capture via Django middleware
|
|
64
|
+
- Full stack trace with code snapshot of the offending file
|
|
65
|
+
- File name, line number, and exact line that raised the exception
|
|
66
|
+
- Request context: URL path, HTTP method, status code, IP address, user agent, and masked request body
|
|
67
|
+
- Source detection (`backend`, `frontend`, `unknown`) via the `X-Client-Type` header
|
|
68
|
+
- Smart deduplication: identical exceptions increment a `count` field
|
|
69
|
+
- AI-powered fix suggestions via OpenAI ChatGPT
|
|
70
|
+
- Resolution tracking with `is_resolved`, `resolved_by`, `resolved_at`, and `resolution_note`
|
|
71
|
+
- REST API with superuser-only access
|
|
72
|
+
- Singleton `AppSettings` model
|
|
73
|
+
|
|
74
|
+
## Requirements
|
|
75
|
+
|
|
76
|
+
- Python >= 3.8
|
|
77
|
+
- Django >= 3.2
|
|
78
|
+
- djangorestframework >= 3.12
|
|
79
|
+
- openai >= 1.3.7
|
|
80
|
+
|
|
81
|
+
## Installation
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install exceptbot-drf
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Setup
|
|
88
|
+
|
|
89
|
+
### 1. Add to `INSTALLED_APPS`
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
INSTALLED_APPS = [
|
|
93
|
+
# ...
|
|
94
|
+
'rest_framework',
|
|
95
|
+
'exceptbot',
|
|
96
|
+
# ...
|
|
97
|
+
]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### 2. Add the middleware
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
MIDDLEWARE = [
|
|
104
|
+
# ...
|
|
105
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
106
|
+
# ...
|
|
107
|
+
'exceptbot.middleware.ExceptBotMiddleware',
|
|
108
|
+
]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`AuthenticationMiddleware` must appear before `ExceptBotMiddleware` so that `request.user` is available.
|
|
112
|
+
|
|
113
|
+
### 3. Configure DRF authentication
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
REST_FRAMEWORK = {
|
|
117
|
+
'DEFAULT_AUTHENTICATION_CLASSES': [
|
|
118
|
+
'rest_framework.authentication.SessionAuthentication',
|
|
119
|
+
'rest_framework.authentication.TokenAuthentication',
|
|
120
|
+
],
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 4. Include the URLs
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
from django.urls import path, include
|
|
128
|
+
|
|
129
|
+
urlpatterns = [
|
|
130
|
+
# ...
|
|
131
|
+
path('exceptbot/', include('exceptbot.urls', namespace='exceptbot')),
|
|
132
|
+
# ...
|
|
133
|
+
]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### 5. Run migrations
|
|
137
|
+
|
|
138
|
+
```bash
|
|
139
|
+
python manage.py migrate exceptbot
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 6. Configure settings
|
|
143
|
+
|
|
144
|
+
Navigate to `/admin/exceptbot/appsettings/` and set the following fields:
|
|
145
|
+
|
|
146
|
+
| Field | Description |
|
|
147
|
+
| :--- | :--- |
|
|
148
|
+
| `project_name` | The directory name of your project (used to locate the correct file in the traceback) |
|
|
149
|
+
| `base_url` | Base URL of your site (e.g., `https://myapp.com`) |
|
|
150
|
+
| `openai_api_key` | OpenAI API key |
|
|
151
|
+
|
|
152
|
+
## API Endpoints
|
|
153
|
+
|
|
154
|
+
| Method | Endpoint | Description |
|
|
155
|
+
| :--- | :--- | :--- |
|
|
156
|
+
| GET | `/exceptbot/api/unresolved/` | List unresolved exceptions |
|
|
157
|
+
| GET | `/exceptbot/api/resolved/` | List resolved exceptions |
|
|
158
|
+
| GET | `/exceptbot/api/<id>/` | Full exception detail |
|
|
159
|
+
| GET | `/exceptbot/api/<id>/error/` | Error message and traceback |
|
|
160
|
+
| GET | `/exceptbot/api/<id>/file/` | Content of the offending file |
|
|
161
|
+
| POST | `/exceptbot/api/<id>/resolve/` | Mark as resolved |
|
|
162
|
+
| POST | `/exceptbot/api/<id>/unresolve/` | Revert to unresolved |
|
|
163
|
+
| POST | `/exceptbot/api/<id>/ai/` | Get or generate AI suggestion |
|
|
164
|
+
| GET | `/exceptbot/api/settings/` | View settings |
|
|
165
|
+
| PUT | `/exceptbot/api/settings/` | Update settings |
|
|
166
|
+
|
|
167
|
+
All endpoints require superuser authentication.
|
|
168
|
+
|
|
169
|
+
## Examples
|
|
170
|
+
|
|
171
|
+
Fetch unresolved exceptions:
|
|
172
|
+
|
|
173
|
+
```bash
|
|
174
|
+
curl -H "Authorization: Bearer YOUR_TOKEN" \
|
|
175
|
+
http://localhost:8000/exceptbot/api/unresolved/
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Mark an exception as resolved:
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
curl -X POST \
|
|
182
|
+
-H "Authorization: Bearer YOUR_TOKEN" \
|
|
183
|
+
-H "Content-Type: application/json" \
|
|
184
|
+
-d '{"resolution_note": "Fixed in commit abc123"}' \
|
|
185
|
+
http://localhost:8000/exceptbot/api/1/resolve/
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
Request an AI suggestion:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
curl -X POST \
|
|
192
|
+
-H "Authorization: Bearer YOUR_TOKEN" \
|
|
193
|
+
http://localhost:8000/exceptbot/api/1/ai/
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Source Detection
|
|
197
|
+
|
|
198
|
+
To distinguish frontend errors from backend errors, include the following header in requests:
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
X-Client-Type: frontend
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
If omitted, the default value is `backend`.
|
|
205
|
+
|
|
206
|
+
## How It Works
|
|
207
|
+
|
|
208
|
+
1. The middleware catches exceptions in `process_exception` and inspects the traceback.
|
|
209
|
+
2. It walks the traceback in reverse to find the first frame inside the project, matched by `project_name`.
|
|
210
|
+
3. It reads the offending file and stores the full source as a code snapshot.
|
|
211
|
+
4. It extracts HTTP method, status code, IP address, user agent, and masked request body.
|
|
212
|
+
5. Identical exceptions increment `count` instead of creating new records.
|
|
213
|
+
6. Superusers query the REST API to view, analyze, and resolve exceptions.
|
|
214
|
+
|
|
215
|
+
## Security
|
|
216
|
+
|
|
217
|
+
- Sensitive request fields (`password`, `token`, `api_key`, `authorization`, and similar) are replaced with `***MASKED***` before being stored.
|
|
218
|
+
- All endpoints are protected by the `IsSuperUser` permission class.
|
|
219
|
+
- The OpenAI API key is hidden in the admin list view and only shown as a boolean.
|
|
220
|
+
- Only one `AppSettings` record can exist.
|
|
221
|
+
|
|
222
|
+
## Project Structure
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
exceptbot/
|
|
226
|
+
├── migrations/
|
|
227
|
+
├── admin.py
|
|
228
|
+
├── apps.py
|
|
229
|
+
├── middleware.py
|
|
230
|
+
├── models.py
|
|
231
|
+
├── permissions.py
|
|
232
|
+
├── serializers.py
|
|
233
|
+
├── urls.py
|
|
234
|
+
└── views.py
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Contributing
|
|
238
|
+
|
|
239
|
+
Issues and pull requests are welcome at:
|
|
240
|
+
https://github.com/tahazarei777/exceptbot-drf
|
|
241
|
+
|
|
242
|
+
## Reporting Issues
|
|
243
|
+
|
|
244
|
+
Report bugs at:
|
|
245
|
+
https://github.com/tahazarei777/exceptbot-drf/issues
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
This project is a fork of [ExceptBot](https://github.com/geneffects/exceptbot) by Brian Risk.
|
|
250
|
+
|
|
251
|
+
Original work: Copyright © 2023-present, [D.AT Analytics, LLC](https://d.at/). All rights reserved.
|
|
252
|
+
|
|
253
|
+
Modifications: Copyright © 2025, Taha Zarei.
|
|
254
|
+
|
|
255
|
+
Licensed under the BSD 3-Clause License. See [LICENSE.md](LICENSE.md) for details.
|
|
256
|
+
Exception Logger with AI Suggestions for Django REST Framework
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
**Website:**
|
|
260
|
+
```
|
|
261
|
+
https://pypi.org/project/exceptbot-drf/
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
**Topics:**
|
|
265
|
+
```
|
|
266
|
+
django
|
|
267
|
+
django-rest-framework
|
|
268
|
+
drf
|
|
269
|
+
exception-logger
|
|
270
|
+
error-tracking
|
|
271
|
+
openai
|
|
272
|
+
chatgpt
|
|
273
|
+
python
|
|
274
|
+
middleware
|
|
275
|
+
api
|
|
276
|
+
```
|
|
277
|
+
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
```
|
|
2
|
+
# ExceptBot DRF
|
|
3
|
+
|
|
4
|
+
Exception Logger with AI Suggestions for Django REST Framework.
|
|
5
|
+
|
|
6
|
+
[](https://pypi.org/project/exceptbot-drf/)
|
|
7
|
+
[](https://www.python.org/)
|
|
8
|
+
[](https://www.djangoproject.com/)
|
|
9
|
+
[](https://www.django-rest-framework.org/)
|
|
10
|
+
[](LICENSE.md)
|
|
11
|
+
|
|
12
|
+
## Overview
|
|
13
|
+
|
|
14
|
+
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.
|
|
15
|
+
|
|
16
|
+
This project is a fork of [ExceptBot](https://github.com/geneffects/exceptbot) by Brian Risk, modified to support Django REST Framework APIs, custom user models, and enhanced request metadata.
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- Automatic exception capture via Django middleware
|
|
21
|
+
- Full stack trace with code snapshot of the offending file
|
|
22
|
+
- File name, line number, and exact line that raised the exception
|
|
23
|
+
- Request context: URL path, HTTP method, status code, IP address, user agent, and masked request body
|
|
24
|
+
- Source detection (`backend`, `frontend`, `unknown`) via the `X-Client-Type` header
|
|
25
|
+
- Smart deduplication: identical exceptions increment a `count` field
|
|
26
|
+
- AI-powered fix suggestions via OpenAI ChatGPT
|
|
27
|
+
- Resolution tracking with `is_resolved`, `resolved_by`, `resolved_at`, and `resolution_note`
|
|
28
|
+
- REST API with superuser-only access
|
|
29
|
+
- Singleton `AppSettings` model
|
|
30
|
+
|
|
31
|
+
## Requirements
|
|
32
|
+
|
|
33
|
+
- Python >= 3.8
|
|
34
|
+
- Django >= 3.2
|
|
35
|
+
- djangorestframework >= 3.12
|
|
36
|
+
- openai >= 1.3.7
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install exceptbot-drf
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Setup
|
|
45
|
+
|
|
46
|
+
### 1. Add to `INSTALLED_APPS`
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
INSTALLED_APPS = [
|
|
50
|
+
# ...
|
|
51
|
+
'rest_framework',
|
|
52
|
+
'exceptbot',
|
|
53
|
+
# ...
|
|
54
|
+
]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 2. Add the middleware
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
MIDDLEWARE = [
|
|
61
|
+
# ...
|
|
62
|
+
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
63
|
+
# ...
|
|
64
|
+
'exceptbot.middleware.ExceptBotMiddleware',
|
|
65
|
+
]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`AuthenticationMiddleware` must appear before `ExceptBotMiddleware` so that `request.user` is available.
|
|
69
|
+
|
|
70
|
+
### 3. Configure DRF authentication
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
REST_FRAMEWORK = {
|
|
74
|
+
'DEFAULT_AUTHENTICATION_CLASSES': [
|
|
75
|
+
'rest_framework.authentication.SessionAuthentication',
|
|
76
|
+
'rest_framework.authentication.TokenAuthentication',
|
|
77
|
+
],
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 4. Include the URLs
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
from django.urls import path, include
|
|
85
|
+
|
|
86
|
+
urlpatterns = [
|
|
87
|
+
# ...
|
|
88
|
+
path('exceptbot/', include('exceptbot.urls', namespace='exceptbot')),
|
|
89
|
+
# ...
|
|
90
|
+
]
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 5. Run migrations
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
python manage.py migrate exceptbot
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 6. Configure settings
|
|
100
|
+
|
|
101
|
+
Navigate to `/admin/exceptbot/appsettings/` and set the following fields:
|
|
102
|
+
|
|
103
|
+
| Field | Description |
|
|
104
|
+
| :--- | :--- |
|
|
105
|
+
| `project_name` | The directory name of your project (used to locate the correct file in the traceback) |
|
|
106
|
+
| `base_url` | Base URL of your site (e.g., `https://myapp.com`) |
|
|
107
|
+
| `openai_api_key` | OpenAI API key |
|
|
108
|
+
|
|
109
|
+
## API Endpoints
|
|
110
|
+
|
|
111
|
+
| Method | Endpoint | Description |
|
|
112
|
+
| :--- | :--- | :--- |
|
|
113
|
+
| GET | `/exceptbot/api/unresolved/` | List unresolved exceptions |
|
|
114
|
+
| GET | `/exceptbot/api/resolved/` | List resolved exceptions |
|
|
115
|
+
| GET | `/exceptbot/api/<id>/` | Full exception detail |
|
|
116
|
+
| GET | `/exceptbot/api/<id>/error/` | Error message and traceback |
|
|
117
|
+
| GET | `/exceptbot/api/<id>/file/` | Content of the offending file |
|
|
118
|
+
| POST | `/exceptbot/api/<id>/resolve/` | Mark as resolved |
|
|
119
|
+
| POST | `/exceptbot/api/<id>/unresolve/` | Revert to unresolved |
|
|
120
|
+
| POST | `/exceptbot/api/<id>/ai/` | Get or generate AI suggestion |
|
|
121
|
+
| GET | `/exceptbot/api/settings/` | View settings |
|
|
122
|
+
| PUT | `/exceptbot/api/settings/` | Update settings |
|
|
123
|
+
|
|
124
|
+
All endpoints require superuser authentication.
|
|
125
|
+
|
|
126
|
+
## Examples
|
|
127
|
+
|
|
128
|
+
Fetch unresolved exceptions:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
curl -H "Authorization: Bearer YOUR_TOKEN" \
|
|
132
|
+
http://localhost:8000/exceptbot/api/unresolved/
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Mark an exception as resolved:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
curl -X POST \
|
|
139
|
+
-H "Authorization: Bearer YOUR_TOKEN" \
|
|
140
|
+
-H "Content-Type: application/json" \
|
|
141
|
+
-d '{"resolution_note": "Fixed in commit abc123"}' \
|
|
142
|
+
http://localhost:8000/exceptbot/api/1/resolve/
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Request an AI suggestion:
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
curl -X POST \
|
|
149
|
+
-H "Authorization: Bearer YOUR_TOKEN" \
|
|
150
|
+
http://localhost:8000/exceptbot/api/1/ai/
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Source Detection
|
|
154
|
+
|
|
155
|
+
To distinguish frontend errors from backend errors, include the following header in requests:
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
X-Client-Type: frontend
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
If omitted, the default value is `backend`.
|
|
162
|
+
|
|
163
|
+
## How It Works
|
|
164
|
+
|
|
165
|
+
1. The middleware catches exceptions in `process_exception` and inspects the traceback.
|
|
166
|
+
2. It walks the traceback in reverse to find the first frame inside the project, matched by `project_name`.
|
|
167
|
+
3. It reads the offending file and stores the full source as a code snapshot.
|
|
168
|
+
4. It extracts HTTP method, status code, IP address, user agent, and masked request body.
|
|
169
|
+
5. Identical exceptions increment `count` instead of creating new records.
|
|
170
|
+
6. Superusers query the REST API to view, analyze, and resolve exceptions.
|
|
171
|
+
|
|
172
|
+
## Security
|
|
173
|
+
|
|
174
|
+
- Sensitive request fields (`password`, `token`, `api_key`, `authorization`, and similar) are replaced with `***MASKED***` before being stored.
|
|
175
|
+
- All endpoints are protected by the `IsSuperUser` permission class.
|
|
176
|
+
- The OpenAI API key is hidden in the admin list view and only shown as a boolean.
|
|
177
|
+
- Only one `AppSettings` record can exist.
|
|
178
|
+
|
|
179
|
+
## Project Structure
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
exceptbot/
|
|
183
|
+
├── migrations/
|
|
184
|
+
├── admin.py
|
|
185
|
+
├── apps.py
|
|
186
|
+
├── middleware.py
|
|
187
|
+
├── models.py
|
|
188
|
+
├── permissions.py
|
|
189
|
+
├── serializers.py
|
|
190
|
+
├── urls.py
|
|
191
|
+
└── views.py
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Contributing
|
|
195
|
+
|
|
196
|
+
Issues and pull requests are welcome at:
|
|
197
|
+
https://github.com/tahazarei777/exceptbot-drf
|
|
198
|
+
|
|
199
|
+
## Reporting Issues
|
|
200
|
+
|
|
201
|
+
Report bugs at:
|
|
202
|
+
https://github.com/tahazarei777/exceptbot-drf/issues
|
|
203
|
+
|
|
204
|
+
## License
|
|
205
|
+
|
|
206
|
+
This project is a fork of [ExceptBot](https://github.com/geneffects/exceptbot) by Brian Risk.
|
|
207
|
+
|
|
208
|
+
Original work: Copyright © 2023-present, [D.AT Analytics, LLC](https://d.at/). All rights reserved.
|
|
209
|
+
|
|
210
|
+
Modifications: Copyright © 2025, Taha Zarei.
|
|
211
|
+
|
|
212
|
+
Licensed under the BSD 3-Clause License. See [LICENSE.md](LICENSE.md) for details.
|
|
213
|
+
Exception Logger with AI Suggestions for Django REST Framework
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
**Website:**
|
|
217
|
+
```
|
|
218
|
+
https://pypi.org/project/exceptbot-drf/
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
**Topics:**
|
|
222
|
+
```
|
|
223
|
+
django
|
|
224
|
+
django-rest-framework
|
|
225
|
+
drf
|
|
226
|
+
exception-logger
|
|
227
|
+
error-tracking
|
|
228
|
+
openai
|
|
229
|
+
chatgpt
|
|
230
|
+
python
|
|
231
|
+
middleware
|
|
232
|
+
api
|
|
233
|
+
```
|
|
234
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
default_app_config = 'exceptbot.apps.ExceptBotConfig'
|