Skip to content

Commit 33a1acf

Browse files
committed
Update README.md
1 parent 0a4d27a commit 33a1acf

File tree

2 files changed

+13
-77
lines changed

2 files changed

+13
-77
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@ Changes:
1616

1717
Changes:
1818

19-
- Add docstrings to handlers
19+
- Add docstrings
2020
- Improve Makefile
2121
- Improve README
2222

2323
## [1.0.3] - 2025-03-16
2424

2525
Changes:
2626

27-
- Update README
28-
- Improve tests
2927
- Fix DRF API settings initialization
28+
- Improve tests
29+
- Update README
3030

3131
## [2.0.0] - 2025-07-11
3232

3333
Breaking changes:
3434

35-
- The API error response now always includes the keys: `title`, `detail`, and `invalid_param`. The `title` key is always populated, while `detail` and `invalid_param` may be `null` depending on the error source.
35+
- The API error response now **always** includes the keys: `title`, `detail`, and `invalid_param`. The `title` key is always populated, while `detail` and `invalid_param` may be `null` depending on the error source.
3636
- Drop support for python 3.8
3737

3838
Changes:
3939

4040
- Improve code modularity and readability
41+
- Split tests in unittest and integration tests
42+
- Improve test coverage
4143
- Update Makefile
4244
- Update README

README.md

Lines changed: 7 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ A library for [Django Rest Framework](https://www.django-rest-framework.org/) re
1212
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.
1313
Error messages are formatted using RFC7807 keywords and DRF exception data.
1414

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.
15+
Unlike standard DRF, where the error response format varies depending on the error source, this library always returns errors in a consistent, predictable structure to make them easier to handle and parse.
1616

1717
## What's different?
1818

@@ -134,7 +134,7 @@ API error messages will include the following keys:
134134

135135
## Settings
136136

137-
Default available settings:
137+
Default settings:
138138

139139
```python
140140
DRF_SIMPLE_API_ERRORS = {
@@ -171,89 +171,23 @@ If `CAMELIZE` is set to `True`:
171171

172172
Support for exceptions that differ from the standard structure of the Django Rest Framework.
173173

174-
For instance, you may want to specify you own exception:
174+
For example, if you need to customize how a specific exception is handled or want to format an existing exception differently, you can create your own handler.
175175

176-
```python
177-
class AuthenticationFailed(exceptions.AuthenticationFailed):
178-
def __init__(self, detail=None, code=None):
179-
"""
180-
Builds a detail dictionary for the error to give more information
181-
to API users.
182-
"""
183-
detail_dict = {"detail": self.default_detail, "code": self.default_code}
184-
185-
if isinstance(detail, dict):
186-
detail_dict.update(detail)
187-
elif detail is not None:
188-
detail_dict["detail"] = detail
189-
190-
if code is not None:
191-
detail_dict["code"] = code
192-
193-
super().__init__(detail_dict)
194-
```
195-
196-
Use exception in code:
197-
198-
```python
199-
def my_func():
200-
raise AuthenticationFailed(
201-
{
202-
"detail": _("Error message."),
203-
"messages": [
204-
{
205-
"metadata": "metadata_data",
206-
"type": "type_name",
207-
"message": "error message",
208-
}
209-
],
210-
}
211-
)
212-
```
213-
214-
This will result in:
215-
216-
```python
217-
AuthenticationFailed(
218-
{
219-
"detail": "Error message.",
220-
"messages": [
221-
{
222-
"metadata": "metadata_data",
223-
"type": "type_name",
224-
"message": "error message",
225-
}
226-
],
227-
}
228-
)
229-
```
230-
231-
You can handle this by creating a `handlers.py` file and specifying an handler for your use case:
232-
233-
```python
234-
def handle_exc_custom_authentication_failed(exc):
235-
from path.to.my.exceptions import AuthenticationFailed
236-
237-
if isinstance(exc, AuthenticationFailed):
238-
try:
239-
exc.detail = exc.detail["messages"][0]["message"]
240-
except (KeyError, IndexError):
241-
exc.detail = exc.detail["detail"]
242-
243-
return exc
244-
```
176+
To customize error handling for your project, simply create a new file (for example, `extra_handlers.py`) and define your own handler functions. This approach lets you tailor error responses to fit your specific needs.
245177

246178
Then add it to the `EXTRA_HANDLERS` list in this package settings:
247179

248180
```python
249181
DRF_SIMPLE_API_ERRORS = {
250182
"EXTRA_HANDLERS": [
251-
"path.to.my.handlers.handle_exc_custom_authentication_failed",
183+
"path.to.my.extra_handlers.custom_handler",
252184
# ...
253185
]
254186
}
255187
```
256188

189+
For reference, this library uses the same pattern for its own extra handlers [here](drf_simple_api_errors/extra_handlers.py).
190+
257191
- #### FIELDS_SEPARATOR
258192

259193
Support for nested dicts containing multiple fields to be flattened.

0 commit comments

Comments
 (0)