8000 Extra docs on custom exception handling. · alumni/django-rest-framework@b6c0c81 · GitHub
[go: up one dir, main page]

Skip to content
< 10000 /div>

Commit b6c0c81

Browse files
committed
Extra docs on custom exception handling.
1 parent 6908c18 commit b6c0c81

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

docs/api-guide/exceptions.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,53 @@ For example, the following request:
2828
Might receive an error response indicating that the `DELETE` method is not allowed on that resource:
2929

3030
HTTP/1.1 405 Method Not Allowed
31-
Content-Type: application/json; charset=utf-8
31+
Content-Type: application/json
3232
Content-Length: 42
3333

3434
{"detail": "Method 'DELETE' not allowed."}
3535

3636
## Custom exception handling
3737

38-
To implement custom exception handling (e.g. to handle additional exception classes or to override the error response format), create an exception handler function with the following signature:
38+
You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects. This allows you to control the style of error responses used by your API.
3939

40-
exception_handler(exc)
40+
The function must take a single argument, which is the exception to be handled, and should either return a `Response` object, or return `None` if the exception cannot be handled. If the handler returns `None` then the exception will be re-raised and Django will return a standard HTTP 500 'server error' response.
4141

42-
* `exc`: The exception.
42+
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
4343

44-
If the function returns `None`, a 500 error will be raised.
44+
HTTP/1.1 405 Method Not Allowed
45+
Content-Type: application/json
46+
Content-Length: 62
47+
48+
{"status_code": 405, "detail": "Method 'DELETE' not allowed."}
49+
50+
In order to alter the style of the response, you could write the following custom exception handler:
51+
52+
from rest_framework.views import exception_handler
53+
54+
def custom_exception_handler(exc):
55+
# Call REST framework's default exception handler first,
56+
# to get the standard error response.
57+
response = exception_handler(exc)
58+
59+
# Now add the HTTP status code to the response.
60+
if response is not None:
61+
response.data['status_code'] = response.status_code
62+
63+
return response
64+
65+
The exception handler must also be configured in your settings, using the `EXCEPTION_HANDLER` setting key. For example:
4566

46-
The exception handler is set globally, using the `EXCEPTION_HANDLER` setting. For example:
67+
REST_FRAMEWORK = {
68+
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
69+
}
4770

48-
'EXCEPTION_HANDLER': 'project.app.module.function'
71+
If not specified, the `'EXCEPTION_HANDLER'` setting defaults to the standard exception handler provided by REST framework:
4972

50-
If not specified, this setting defaults to the exception handler described above:
73+
REST_FRAMEWORK = {
74+
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
75+
}
5176

52-
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
77+
Note that the exception handler will only be called for responses generated by raised exceptions. It will not be used for any responses returned directly by the view, such as the `HTTP_400_BAD_REQUEST` responses that are returned by the generic views when serializer validation fails.
5378

5479
---
5580

0 commit comments

Comments
 (0)
0