1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
%global _empty_manifest_terminate_build 0
Name: python-drf-exceptions-hog
Version: 0.3.0
Release: 1
Summary: Standardized and easy-to-parse API error responses for DRF.
License: MIT License
URL: https://github.com/posthog/drf-exceptions-hog
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/85/54/f60660069a880c86c248aca30f31d5b079a08cae207dbd3eb778af35a7d2/drf-exceptions-hog-0.3.0.tar.gz
BuildArch: noarch
Requires: python3-djangorestframework
%description
<img src="drf-exceptions-hog.png" alt="Logo" height="125" />
# DRF Exceptions Hog
Standardized and **easy-to-parse** API error responses for [Django REST Framework][drf].
After reusing similar code in multiple projects, we realized this might actually help others. The problem we're trying to solve is that DRF exceptions tend to vary in format and therefore require complex parsing logic on the frontend, which generally needs to be implemented in more than one language or framework. This simple package standardizes the exception responses to simplify the parsing logic on the frontend and enable developers to provide clear errors to their users (instead of the cryptic or even shady-looking parsing errors). This package is inspired on the way [Stripe API](https://stripe.com/docs/api/errors) handles errors. See an example below.
You will get predictable responses like these:
```json
// Example 1
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "name"
}
// Example 2
{
"type": "authentication_error",
"code": "permission_denied",
"detail": "You do not have permission to perform this operation.",
"attr": null
}
```
instead of these:
```json
// Example 1
{
"name": ["This field is required."]
}
// Example 2
{
"detail": "You do not have permission to perform this operation."
}
```
**Note:** Currently we only support JSON responses. If you'd like us to support a different response format, please open an issue or a PR (see [Contributing](#-contributing))
## 🔌 Usage
To start using DRF Exceptions Hog please follow these instructions:
Install the package with `pip`
```bash
pip install drf-exceptions-hog
```
Update your DRF configuration on your `settings.py`.
```python
REST_FRAMEWORK={
"EXCEPTION_HANDLER": "exceptions_hog.exception_handler",
}
```
Optionally set additional configuration for the package.
```python
EXCEPTIONS_HOG = {
"EXCEPTION_REPORTING": "exceptions_hog.handler.exception_reporter",
"ENABLE_IN_DEBUG": False,
"NESTED_KEY_SEPARATOR": "__",
"SUPPORT_MULTIPLE_EXCEPTIONS": False,
}
```
- `EXCEPTION_REPORTING`: specify a method to call after an exception occurs. Particularly useful to report errors (e.g. through Sentry, NewRelic, ...). Default: `exceptions_hog.handler.exception_reporter`
- `ENABLE_IN_DEBUG`: whether exceptions-hog should run when `DEBUG = 1`. It's useful to turn this off in debugging to get full error stack traces when developing. Defaut: `False`.
- `NESTED_KEY_SEPARATOR`: customize the separator used for obtaining the `attr` name if the exception comes from nested objects (e.g. nested serializers). Default: `__`.
- `SUPPORT_MULTIPLE_EXCEPTIONS`: whether exceptions-hog should return all exceptions in an error response. Useful particularly in form and serializer validation where multiple input exceptions can occur.
## 📑 Documentation
We're working on more comprehensive documentation. Feel free to open a PR to contribute to this. In the meantime, you will find the most relevant information for this package here.
### Response structure
All responses handled by DRF Exceptions Hog have the following format:
```json
{
"type": "server_error",
"code": "server_error",
"detail": "Something went wrong.",
"attr": null,
"list": null
}
```
where:
- `type` entails the high-level grouping of the type error returned (See [Error Types](#error-types)).
- `code` is a machine-friendly error code specific for this type of error (e.g. `permission_denied`, `method_not_allowed`, `required`)
- `detail` will contain human-friendly information on the error (e.g. "This field is required.", "Authentication credentials were not provided.").
- For security reasons (mainly to avoid leaking sensitive information) this attribute will return a generic error message for unhandled server exceptions, like an `ImportError`.
- If you use Django localization, all our exception detail messages support using multiple languages.
- `attr` will contain the name of the attribute to which the exception is related. Relevant mostly for `validation_error`s.
- `list` will only be returned when [multiple exceptions](#multiple-exceptions) are enabled and the exception contains multiple exceptions (i.e. `type = multiple`).
- `extra` is an extra attribute you can set on an exception to pass through extra content, normally in dict form.
### Multiple exceptions
There are some cases when handling multiple exceptions in a single response can be helpful. For instance, if you have a form with multiple fields, each field can have their own validations, and a user could benefit from knowing everything that is wrong in a single pass. You can enable multiple exception support by setting the `SUPPORT_MULTIPLE_EXCEPTIONS` setting to `True`. When it's enabled, if multiple exceptions are raised (e.g. by a serializer), you will receive a response like this:
```json
{
"type": "multiple",
"code": "multiple",
"detail": "Multiple exceptions ocurred. Please check list for details.",
"attr": null,
"list": [
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "email"
},
{
"type": "validation_error",
"code": "unsafe_password",
"detail": "This password is unsafe.",
"attr": "password"
}
]
}
```
### Error types
Our package introduces the following general error types (but feel free to add custom ones):
- `authentication_error` indicates there is an authentication-related problem with the request (e.g. no authentication credentials provided, invalid or expired credentials provided, credentials have insufficient privileges, etc.)
- `invalid_request` indicates a general issue with the request that must be fixed by the client, excluding validation errors (e.g. request has an invalid media type format, request is malformed, etc.)
- `multiple` indicates multiple exceptions ocurred (only if enabled). See [multiple exceptions](#multiple-exceptions) for details.
- `server_error` indicates a generic internal server error that needs to be addressed on the server.
- `throttled_error` indicates the request is throttled or rate limited and must be retried by the client at a later time.
- `validation_error` indicates the request has not passed validation and must be fixed by the client (e.g. a required attribute was not provided, an incorrect data type was passed for an attribute, etc.)
## 🤝 Contributing
Want to help move this project forward? Read our [CONTRIBUTING.md](CONTRIBUTING.md).
## 👩💻 Development
To run a local copy of the package for development, please follow these instructions:
1. Clone the repository.
1. **[Optional]**. Install and activate a virtual enviroment. Example:
```bash
python3 -m venv env && source env/bin/activate
```
1. Install the project dependencies and the test dependencies.
```bash
python setup.py develop
pip install -r requirements-test.txt
```
1. Run the tests to make sure everything is working as expected.
```bash
python runtests.py
```
1. Start coding!
## 🧱 Requirements
- This package requires at least Python 3.7 & Django 3.1
- Supported Python versions: 3.7.x, 3.8.x & 3.9.x
- Supported Django versions: 3.1.x & 3.2.x
## 👨⚖️ License
We ♥ Open Source! This repository is MIT licensed by PostHog. Full license [here](LICENSE).
[drf]: https://github.com/encode/django-rest-framework
%package -n python3-drf-exceptions-hog
Summary: Standardized and easy-to-parse API error responses for DRF.
Provides: python-drf-exceptions-hog
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-drf-exceptions-hog
<img src="drf-exceptions-hog.png" alt="Logo" height="125" />
# DRF Exceptions Hog
Standardized and **easy-to-parse** API error responses for [Django REST Framework][drf].
After reusing similar code in multiple projects, we realized this might actually help others. The problem we're trying to solve is that DRF exceptions tend to vary in format and therefore require complex parsing logic on the frontend, which generally needs to be implemented in more than one language or framework. This simple package standardizes the exception responses to simplify the parsing logic on the frontend and enable developers to provide clear errors to their users (instead of the cryptic or even shady-looking parsing errors). This package is inspired on the way [Stripe API](https://stripe.com/docs/api/errors) handles errors. See an example below.
You will get predictable responses like these:
```json
// Example 1
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "name"
}
// Example 2
{
"type": "authentication_error",
"code": "permission_denied",
"detail": "You do not have permission to perform this operation.",
"attr": null
}
```
instead of these:
```json
// Example 1
{
"name": ["This field is required."]
}
// Example 2
{
"detail": "You do not have permission to perform this operation."
}
```
**Note:** Currently we only support JSON responses. If you'd like us to support a different response format, please open an issue or a PR (see [Contributing](#-contributing))
## 🔌 Usage
To start using DRF Exceptions Hog please follow these instructions:
Install the package with `pip`
```bash
pip install drf-exceptions-hog
```
Update your DRF configuration on your `settings.py`.
```python
REST_FRAMEWORK={
"EXCEPTION_HANDLER": "exceptions_hog.exception_handler",
}
```
Optionally set additional configuration for the package.
```python
EXCEPTIONS_HOG = {
"EXCEPTION_REPORTING": "exceptions_hog.handler.exception_reporter",
"ENABLE_IN_DEBUG": False,
"NESTED_KEY_SEPARATOR": "__",
"SUPPORT_MULTIPLE_EXCEPTIONS": False,
}
```
- `EXCEPTION_REPORTING`: specify a method to call after an exception occurs. Particularly useful to report errors (e.g. through Sentry, NewRelic, ...). Default: `exceptions_hog.handler.exception_reporter`
- `ENABLE_IN_DEBUG`: whether exceptions-hog should run when `DEBUG = 1`. It's useful to turn this off in debugging to get full error stack traces when developing. Defaut: `False`.
- `NESTED_KEY_SEPARATOR`: customize the separator used for obtaining the `attr` name if the exception comes from nested objects (e.g. nested serializers). Default: `__`.
- `SUPPORT_MULTIPLE_EXCEPTIONS`: whether exceptions-hog should return all exceptions in an error response. Useful particularly in form and serializer validation where multiple input exceptions can occur.
## 📑 Documentation
We're working on more comprehensive documentation. Feel free to open a PR to contribute to this. In the meantime, you will find the most relevant information for this package here.
### Response structure
All responses handled by DRF Exceptions Hog have the following format:
```json
{
"type": "server_error",
"code": "server_error",
"detail": "Something went wrong.",
"attr": null,
"list": null
}
```
where:
- `type` entails the high-level grouping of the type error returned (See [Error Types](#error-types)).
- `code` is a machine-friendly error code specific for this type of error (e.g. `permission_denied`, `method_not_allowed`, `required`)
- `detail` will contain human-friendly information on the error (e.g. "This field is required.", "Authentication credentials were not provided.").
- For security reasons (mainly to avoid leaking sensitive information) this attribute will return a generic error message for unhandled server exceptions, like an `ImportError`.
- If you use Django localization, all our exception detail messages support using multiple languages.
- `attr` will contain the name of the attribute to which the exception is related. Relevant mostly for `validation_error`s.
- `list` will only be returned when [multiple exceptions](#multiple-exceptions) are enabled and the exception contains multiple exceptions (i.e. `type = multiple`).
- `extra` is an extra attribute you can set on an exception to pass through extra content, normally in dict form.
### Multiple exceptions
There are some cases when handling multiple exceptions in a single response can be helpful. For instance, if you have a form with multiple fields, each field can have their own validations, and a user could benefit from knowing everything that is wrong in a single pass. You can enable multiple exception support by setting the `SUPPORT_MULTIPLE_EXCEPTIONS` setting to `True`. When it's enabled, if multiple exceptions are raised (e.g. by a serializer), you will receive a response like this:
```json
{
"type": "multiple",
"code": "multiple",
"detail": "Multiple exceptions ocurred. Please check list for details.",
"attr": null,
"list": [
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "email"
},
{
"type": "validation_error",
"code": "unsafe_password",
"detail": "This password is unsafe.",
"attr": "password"
}
]
}
```
### Error types
Our package introduces the following general error types (but feel free to add custom ones):
- `authentication_error` indicates there is an authentication-related problem with the request (e.g. no authentication credentials provided, invalid or expired credentials provided, credentials have insufficient privileges, etc.)
- `invalid_request` indicates a general issue with the request that must be fixed by the client, excluding validation errors (e.g. request has an invalid media type format, request is malformed, etc.)
- `multiple` indicates multiple exceptions ocurred (only if enabled). See [multiple exceptions](#multiple-exceptions) for details.
- `server_error` indicates a generic internal server error that needs to be addressed on the server.
- `throttled_error` indicates the request is throttled or rate limited and must be retried by the client at a later time.
- `validation_error` indicates the request has not passed validation and must be fixed by the client (e.g. a required attribute was not provided, an incorrect data type was passed for an attribute, etc.)
## 🤝 Contributing
Want to help move this project forward? Read our [CONTRIBUTING.md](CONTRIBUTING.md).
## 👩💻 Development
To run a local copy of the package for development, please follow these instructions:
1. Clone the repository.
1. **[Optional]**. Install and activate a virtual enviroment. Example:
```bash
python3 -m venv env && source env/bin/activate
```
1. Install the project dependencies and the test dependencies.
```bash
python setup.py develop
pip install -r requirements-test.txt
```
1. Run the tests to make sure everything is working as expected.
```bash
python runtests.py
```
1. Start coding!
## 🧱 Requirements
- This package requires at least Python 3.7 & Django 3.1
- Supported Python versions: 3.7.x, 3.8.x & 3.9.x
- Supported Django versions: 3.1.x & 3.2.x
## 👨⚖️ License
We ♥ Open Source! This repository is MIT licensed by PostHog. Full license [here](LICENSE).
[drf]: https://github.com/encode/django-rest-framework
%package help
Summary: Development documents and examples for drf-exceptions-hog
Provides: python3-drf-exceptions-hog-doc
%description help
<img src="drf-exceptions-hog.png" alt="Logo" height="125" />
# DRF Exceptions Hog
Standardized and **easy-to-parse** API error responses for [Django REST Framework][drf].
After reusing similar code in multiple projects, we realized this might actually help others. The problem we're trying to solve is that DRF exceptions tend to vary in format and therefore require complex parsing logic on the frontend, which generally needs to be implemented in more than one language or framework. This simple package standardizes the exception responses to simplify the parsing logic on the frontend and enable developers to provide clear errors to their users (instead of the cryptic or even shady-looking parsing errors). This package is inspired on the way [Stripe API](https://stripe.com/docs/api/errors) handles errors. See an example below.
You will get predictable responses like these:
```json
// Example 1
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "name"
}
// Example 2
{
"type": "authentication_error",
"code": "permission_denied",
"detail": "You do not have permission to perform this operation.",
"attr": null
}
```
instead of these:
```json
// Example 1
{
"name": ["This field is required."]
}
// Example 2
{
"detail": "You do not have permission to perform this operation."
}
```
**Note:** Currently we only support JSON responses. If you'd like us to support a different response format, please open an issue or a PR (see [Contributing](#-contributing))
## 🔌 Usage
To start using DRF Exceptions Hog please follow these instructions:
Install the package with `pip`
```bash
pip install drf-exceptions-hog
```
Update your DRF configuration on your `settings.py`.
```python
REST_FRAMEWORK={
"EXCEPTION_HANDLER": "exceptions_hog.exception_handler",
}
```
Optionally set additional configuration for the package.
```python
EXCEPTIONS_HOG = {
"EXCEPTION_REPORTING": "exceptions_hog.handler.exception_reporter",
"ENABLE_IN_DEBUG": False,
"NESTED_KEY_SEPARATOR": "__",
"SUPPORT_MULTIPLE_EXCEPTIONS": False,
}
```
- `EXCEPTION_REPORTING`: specify a method to call after an exception occurs. Particularly useful to report errors (e.g. through Sentry, NewRelic, ...). Default: `exceptions_hog.handler.exception_reporter`
- `ENABLE_IN_DEBUG`: whether exceptions-hog should run when `DEBUG = 1`. It's useful to turn this off in debugging to get full error stack traces when developing. Defaut: `False`.
- `NESTED_KEY_SEPARATOR`: customize the separator used for obtaining the `attr` name if the exception comes from nested objects (e.g. nested serializers). Default: `__`.
- `SUPPORT_MULTIPLE_EXCEPTIONS`: whether exceptions-hog should return all exceptions in an error response. Useful particularly in form and serializer validation where multiple input exceptions can occur.
## 📑 Documentation
We're working on more comprehensive documentation. Feel free to open a PR to contribute to this. In the meantime, you will find the most relevant information for this package here.
### Response structure
All responses handled by DRF Exceptions Hog have the following format:
```json
{
"type": "server_error",
"code": "server_error",
"detail": "Something went wrong.",
"attr": null,
"list": null
}
```
where:
- `type` entails the high-level grouping of the type error returned (See [Error Types](#error-types)).
- `code` is a machine-friendly error code specific for this type of error (e.g. `permission_denied`, `method_not_allowed`, `required`)
- `detail` will contain human-friendly information on the error (e.g. "This field is required.", "Authentication credentials were not provided.").
- For security reasons (mainly to avoid leaking sensitive information) this attribute will return a generic error message for unhandled server exceptions, like an `ImportError`.
- If you use Django localization, all our exception detail messages support using multiple languages.
- `attr` will contain the name of the attribute to which the exception is related. Relevant mostly for `validation_error`s.
- `list` will only be returned when [multiple exceptions](#multiple-exceptions) are enabled and the exception contains multiple exceptions (i.e. `type = multiple`).
- `extra` is an extra attribute you can set on an exception to pass through extra content, normally in dict form.
### Multiple exceptions
There are some cases when handling multiple exceptions in a single response can be helpful. For instance, if you have a form with multiple fields, each field can have their own validations, and a user could benefit from knowing everything that is wrong in a single pass. You can enable multiple exception support by setting the `SUPPORT_MULTIPLE_EXCEPTIONS` setting to `True`. When it's enabled, if multiple exceptions are raised (e.g. by a serializer), you will receive a response like this:
```json
{
"type": "multiple",
"code": "multiple",
"detail": "Multiple exceptions ocurred. Please check list for details.",
"attr": null,
"list": [
{
"type": "validation_error",
"code": "required",
"detail": "This field is required.",
"attr": "email"
},
{
"type": "validation_error",
"code": "unsafe_password",
"detail": "This password is unsafe.",
"attr": "password"
}
]
}
```
### Error types
Our package introduces the following general error types (but feel free to add custom ones):
- `authentication_error` indicates there is an authentication-related problem with the request (e.g. no authentication credentials provided, invalid or expired credentials provided, credentials have insufficient privileges, etc.)
- `invalid_request` indicates a general issue with the request that must be fixed by the client, excluding validation errors (e.g. request has an invalid media type format, request is malformed, etc.)
- `multiple` indicates multiple exceptions ocurred (only if enabled). See [multiple exceptions](#multiple-exceptions) for details.
- `server_error` indicates a generic internal server error that needs to be addressed on the server.
- `throttled_error` indicates the request is throttled or rate limited and must be retried by the client at a later time.
- `validation_error` indicates the request has not passed validation and must be fixed by the client (e.g. a required attribute was not provided, an incorrect data type was passed for an attribute, etc.)
## 🤝 Contributing
Want to help move this project forward? Read our [CONTRIBUTING.md](CONTRIBUTING.md).
## 👩💻 Development
To run a local copy of the package for development, please follow these instructions:
1. Clone the repository.
1. **[Optional]**. Install and activate a virtual enviroment. Example:
```bash
python3 -m venv env && source env/bin/activate
```
1. Install the project dependencies and the test dependencies.
```bash
python setup.py develop
pip install -r requirements-test.txt
```
1. Run the tests to make sure everything is working as expected.
```bash
python runtests.py
```
1. Start coding!
## 🧱 Requirements
- This package requires at least Python 3.7 & Django 3.1
- Supported Python versions: 3.7.x, 3.8.x & 3.9.x
- Supported Django versions: 3.1.x & 3.2.x
## 👨⚖️ License
We ♥ Open Source! This repository is MIT licensed by PostHog. Full license [here](LICENSE).
[drf]: https://github.com/encode/django-rest-framework
%prep
%autosetup -n drf-exceptions-hog-0.3.0
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-drf-exceptions-hog -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Wed Apr 12 2023 Python_Bot <Python_Bot@openeuler.org> - 0.3.0-1
- Package Spec generated
|