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: docs/api-guide/exceptions.md
+43-11Lines changed: 43 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -98,7 +98,7 @@ Note that the exception handler will only be called for responses generated by r
98
98
99
99
The **base class** for all exceptions raised inside an `APIView` class or `@api_view`.
100
100
101
-
To provide a custom exception, subclass `APIException` and set the `.status_code` and `.default_detail` properties on the class.
101
+
To provide a custom exception, subclass `APIException` and set the `.status_code`, `.default_detail`, and `default_code` attributes on the class.
102
102
103
103
For example, if your API relies on a third party service that may sometimes be unreachable, you might want to implement an exception for the "503 Service Unavailable" HTTP response code. You could do this like so:
104
104
@@ -107,82 +107,114 @@ For example, if your API relies on a third party service that may sometimes be u
107
107
class ServiceUnavailable(APIException):
108
108
status_code = 503
109
109
default_detail = 'Service temporarily unavailable, try again later.'
110
+
default_code = 'service_unavailable'
111
+
112
+
#### Inspecting API exceptions
113
+
114
+
There are a number of different properties available for inspecting the status
115
+
of an API exception. You can use these to build custom exception handling
116
+
for your project.
117
+
118
+
The available attributes and methods are:
119
+
120
+
*`.detail` - Return the textual description of the error.
121
+
*`.get_codes()` - Return the code identifier of the error.
122
+
*`.full_details()` - Return both the textual description and the code identifier.
123
+
124
+
In most cases the error detail will be a simple item:
125
+
126
+
>>> print(exc.detail)
127
+
You do not have permission to perform this action.
128
+
>>> print(exc.get_codes())
129
+
permission_denied
130
+
>>> print(exc.full_details())
131
+
{'message':'You do not have permission to perform this action.','code':'permission_denied'}
132
+
133
+
In the case of validation errors the error detail will be either a list or
134
+
dictionary of items:
135
+
136
+
>>> print(exc.detail)
137
+
{"name":"This field is required.","age":"A valid integer is required."}
138
+
>>> print(exc.get_codes())
139
+
{"name":"required","age":"invalid"}
140
+
>>> print(exc.get_full_details())
141
+
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}
Raised when an incoming request includes incorrect authentication.
124
156
125
157
By default this exception results in a response with the HTTP status code "401 Unauthenticated", but it may also result in a "403 Forbidden" response, depending on the authentication scheme in use. See the [authentication documentation][authentication] for more details.
Raised when an unauthenticated request fails the permission checks.
132
164
133
165
By default this exception results in a response with the HTTP status code "401 Unauthenticated", but it may also result in a "403 Forbidden" response, depending on the authentication scheme in use. See the [authentication documentation][authentication] for more details.
0 commit comments