You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+42-28Lines changed: 42 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,15 +9,18 @@
9
9
10
10
A library for [Django Rest Framework](https://www.django-rest-framework.org/) returning **consistent, predictable and easy-to-parse API error messages**.
11
11
12
-
This library was built with [RFC7807](https://tools.ietf.org/html/rfc7807) guidelines in mind, but with a small twist: it defines a "problem detail" as a list, but it still serves as a way to include errors in a predictable and easy-to-parse format for any API consumer. Error messages are formatted using RFC7807 keywords and DRF exception data.
12
+
This library was built with [RFC7807](https://tools.ietf.org/html/rfc7807) guidelines in mind, but with a small twist: it defines a "problem detail" as a list instead of a string, but it still serves as a way to include errors in a human-readable and easy-to-parse format for any API consumer.
13
+
Error messages are formatted using RFC7807 keywords and DRF exception data.
14
+
15
+
This library always returns errors in a consistent, predictable structure, making them easier to handle and parse, unlike standard DRF, where error response formats vary depending on the error source.
13
16
14
17
## What's different?
15
18
16
19
Compared to other similar and popular libraries, this library:
17
20
18
21
- Is based on RFC7807 guidelines
19
22
- Aims to provide not only a standardized format for error details, but also human-readable error messages (perfect for both internal and public APIs)
20
-
- Transforms both `django.core.exceptions.ValidationError` and `rest_framework.errors.ValidationError` to API errors, so you don't have to handle error raised by services/domain logic, `clean()`, or other functions/methods
23
+
- Transforms both `django.core.exceptions.ValidationError` and `rest_framework.errors.ValidationError` to API errors, so you don't have to handle error raised by services/domain logic, `clean()`, etc.
21
24
22
25
## Table of Contents
23
26
@@ -57,13 +60,13 @@ REST_FRAMEWORK = {
57
60
58
61
### Error structure overview
59
62
60
-
API error messages typically include the following keys:
63
+
API error messages will include the following keys:
61
64
62
-
-`"title"` (`str`): A brief summary that describes the problem type
63
-
-`"detail"` (`list[str] | None`): A list of specific explanations related to the problem
64
-
-`"invalid_params"` (`list[dict] | None`): A list of dict containing details about parameters that were invalid or malformed in the request. Each dict within this list provides:
65
-
-`"name"` (`str`): The name of the parameter that was found to be invalid
66
-
-`"reasons"` (`list[str]`): A list of strings describing the specific reasons why the parameter was considered invalid or malformed
65
+
-`"title"` (`str`): A brief summary that describes the problem type.
66
+
-`"detail"` (`list[str] | None`): A list of specific explanations related to the problem, if any.
67
+
-`"invalid_params"` (`list[dict] | None`): A list of dict containing details about parameters that were invalid or malformed in the request, if any. Each dict within this list provides:
68
+
-`"name"` (`str`): The name of the parameter that was found to be invalid.
69
+
-`"reasons"` (`list[str]`): A list of strings describing the specific reasons why the parameter was considered invalid or malformed.
67
70
68
71
```json
69
72
{
@@ -91,17 +94,18 @@ API error messages typically include the following keys:
91
94
92
95
```json
93
96
{
94
-
"title": "Error message.",
95
-
"invalid_params": [
96
-
{
97
-
"name": "field_name",
98
-
"reason": [
99
-
"error",
100
-
...
101
-
]
102
-
},
103
-
...
104
-
]
97
+
"title": "Error message.",
98
+
"details": null,
99
+
"invalid_params": [
100
+
{
101
+
"name": "field_name",
102
+
"reason": [
103
+
"error"
104
+
// ...
105
+
]
106
+
}
107
+
// ...
108
+
]
105
109
}
106
110
```
107
111
@@ -111,17 +115,20 @@ API error messages typically include the following keys:
111
115
{
112
116
"title": "Error message.",
113
117
"detail": [
114
-
"error",
115
-
...
116
-
]
118
+
"error"
119
+
// ...
120
+
],
121
+
"invalid_params": null
117
122
}
118
123
```
119
124
120
125
#### Other bad requests with no detail
121
126
122
127
```json
123
128
{
124
-
"title": "Error message."
129
+
"title": "Error message.",
130
+
"detail": null,
131
+
"invalid_params": null
125
132
}
126
133
```
127
134
@@ -146,15 +153,16 @@ If `CAMELIZE` is set to `True`:
146
153
```json
147
154
{
148
155
"title": "Error message.",
156
+
"details": null,
149
157
"invalidParams": [
150
158
{
151
159
"name": "fieldName",
152
160
"reason": [
153
-
"error",
154
-
...
161
+
"error"
162
+
// ...
155
163
]
156
164
}
157
-
...
165
+
// ...
158
166
]
159
167
}
160
168
```
@@ -274,13 +282,19 @@ All the necessary commands are included in the `Makefile`.
274
282
275
283
We are using `tox` and `poetry` to run tests in every supported Python version.
276
284
277
-
Run test with the commands below:
285
+
Run test during development with the commands below:
278
286
279
287
```
280
-
make install
288
+
make install # only if necessary
281
289
make test
282
290
```
283
291
292
+
Finally, run `tox` to ensure the changes work for every supported python version:
293
+
294
+
```
295
+
tox -v
296
+
```
297
+
284
298
## Support
285
299
286
300
Please [open an issue](https://github.com/gripep/drf-simple-api-errors/issues/new).
0 commit comments