exceptions.py
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
— Doug Hellmann, Python Exception Handling Techniques
Exception handling in REST framework views
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
The handled exceptions are:
- Subclasses of
APIException
raised inside REST framework. - Django’s
Http404
exception. - Django’s
PermissionDenied
exception.
In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.
Most error responses will include a key detail
in the body of the response.
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json
Might receive an error response indicating that the DELETE
method is not allowed on that resource:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42
{"detail": "Method 'DELETE' not allowed."}
Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY
setting.
An example validation error might look like this:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94
{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}
Custom exception handling
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.
The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function 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.
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62
{"status_code": 405, "detail": "Method 'DELETE' not allowed."}
In order to alter the style of the response, you could write the following custom exception handler:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view']
.
The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER
setting key. For example:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
If not specified, the 'EXCEPTION_HANDLER'
setting defaults to the standard exception handler provided by REST framework:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}
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.
API Reference
APIException
Signature: APIException()
The base class for all exceptions raised inside an APIView
class or @api_view
.
To provide a custom exception, subclass APIException
and set the .status_code
, .default_detail
, and default_code
attributes on the class.
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:
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
default_code = 'service_unavailable'
Inspecting API exceptions
There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.
The available attributes and methods are:
.detail
— Return the textual description of the error..get_codes()
— Return the code identifier of the error..get_full_details()
— Return both the textual description and the code identifier.
In most cases the error detail will be a simple item:
>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}
In the case of validation errors the error detail will be either a list or
dictionary of items:
>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}
ParseError
Signature: ParseError(detail=None, code=None)
Raised if the request contains malformed data when accessing request.data
.
By default this exception results in a response with the HTTP status code «400 Bad Request».
AuthenticationFailed
Signature: AuthenticationFailed(detail=None, code=None)
Raised when an incoming request includes incorrect authentication.
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 for more details.
NotAuthenticated
Signature: NotAuthenticated(detail=None, code=None)
Raised when an unauthenticated request fails the permission checks.
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 for more details.
PermissionDenied
Signature: PermissionDenied(detail=None, code=None)
Raised when an authenticated request fails the permission checks.
By default this exception results in a response with the HTTP status code «403 Forbidden».
NotFound
Signature: NotFound(detail=None, code=None)
Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404
Django exception.
By default this exception results in a response with the HTTP status code «404 Not Found».
MethodNotAllowed
Signature: MethodNotAllowed(method, detail=None, code=None)
Raised when an incoming request occurs that does not map to a handler method on the view.
By default this exception results in a response with the HTTP status code «405 Method Not Allowed».
NotAcceptable
Signature: NotAcceptable(detail=None, code=None)
Raised when an incoming request occurs with an Accept
header that cannot be satisfied by any of the available renderers.
By default this exception results in a response with the HTTP status code «406 Not Acceptable».
Signature: UnsupportedMediaType(media_type, detail=None, code=None)
Raised if there are no parsers that can handle the content type of the request data when accessing request.data
.
By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».
Throttled
Signature: Throttled(wait=None, detail=None, code=None)
Raised when an incoming request fails the throttling checks.
By default this exception results in a response with the HTTP status code «429 Too Many Requests».
ValidationError
Signature: ValidationError(detail, code=None)
The ValidationError
exception is slightly different from the other APIException
classes:
- The
detail
argument is mandatory, not optional. - The
detail
argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in thevalidate()
method of a serializer. For example.raise serializers.ValidationError({'name': 'Please enter a valid name.'})
- By convention you should import the serializers module and use a fully qualified
ValidationError
style, in order to differentiate it from Django’s built-in validation error. For example.raise serializers.ValidationError('This field must be an integer value.')
The ValidationError
class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid
with the raise_exception
keyword argument:
serializer.is_valid(raise_exception=True)
The generic views use the raise_exception=True
flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.
By default this exception results in a response with the HTTP status code «400 Bad Request».
Generic Error Views
Django REST Framework provides two error views suitable for providing generic JSON 500
Server Error and
400
Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)
Use these as per Django’s Customizing error views documentation.
rest_framework.exceptions.server_error
Returns a response with status code 500
and application/json
content type.
Set as handler500
:
handler500 = 'rest_framework.exceptions.server_error'
rest_framework.exceptions.bad_request
Returns a response with status code 400
and application/json
content type.
Set as handler400
:
handler400 = 'rest_framework.exceptions.bad_request'
Third party packages
The following third-party packages are also available.
DRF Standardized Errors
The drf-standardized-errors package provides an exception handler that generates the same format for all 4xx and 5xx responses. It is a drop-in replacement for the default exception handler and allows customizing the error response format without rewriting the whole exception handler. The standardized error response format is easier to document and easier to handle by API consumers.
source |
---|
exceptions.py |
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
— Doug Hellmann, Python Exception Handling Techniques
Exception handling in REST framework views
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
The handled exceptions are:
- Subclasses of
APIException
raised inside REST framework. - Django’s
Http404
exception. - Django’s
PermissionDenied
exception.
In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.
Most error responses will include a key detail
in the body of the response.
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json
Might receive an error response indicating that the DELETE
method is not allowed on that resource:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42
{"detail": "Method 'DELETE' not allowed."}
Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY
setting.
An example validation error might look like this:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94
{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}
Custom exception handling
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.
The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function 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.
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62
{"status_code": 405, "detail": "Method 'DELETE' not allowed."}
In order to alter the style of the response, you could write the following custom exception handler:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view']
.
The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER
setting key. For example:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
If not specified, the 'EXCEPTION_HANDLER'
setting defaults to the standard exception handler provided by REST framework:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}
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.
API Reference
APIException
Signature: APIException()
The base class for all exceptions raised inside an APIView
class or @api_view
.
To provide a custom exception, subclass APIException
and set the .status_code
, .default_detail
, and default_code
attributes on the class.
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:
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
default_code = 'service_unavailable'
Inspecting API exceptions
There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.
The available attributes and methods are:
.detail
— Return the textual description of the error..get_codes()
— Return the code identifier of the error..get_full_details()
— Return both the textual description and the code identifier.
In most cases the error detail will be a simple item:
>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}
In the case of validation errors the error detail will be either a list or
dictionary of items:
>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}
ParseError
Signature: ParseError(detail=None, code=None)
Raised if the request contains malformed data when accessing request.data
.
By default this exception results in a response with the HTTP status code «400 Bad Request».
AuthenticationFailed
Signature: AuthenticationFailed(detail=None, code=None)
Raised when an incoming request includes incorrect authentication.
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 for more details.
NotAuthenticated
Signature: NotAuthenticated(detail=None, code=None)
Raised when an unauthenticated request fails the permission checks.
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 for more details.
PermissionDenied
Signature: PermissionDenied(detail=None, code=None)
Raised when an authenticated request fails the permission checks.
By default this exception results in a response with the HTTP status code «403 Forbidden».
NotFound
Signature: NotFound(detail=None, code=None)
Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404
Django exception.
By default this exception results in a response with the HTTP status code «404 Not Found».
MethodNotAllowed
Signature: MethodNotAllowed(method, detail=None, code=None)
Raised when an incoming request occurs that does not map to a handler method on the view.
By default this exception results in a response with the HTTP status code «405 Method Not Allowed».
NotAcceptable
Signature: NotAcceptable(detail=None, code=None)
Raised when an incoming request occurs with an Accept
header that cannot be satisfied by any of the available renderers.
By default this exception results in a response with the HTTP status code «406 Not Acceptable».
UnsupportedMediaType
Signature: UnsupportedMediaType(media_type, detail=None, code=None)
Raised if there are no parsers that can handle the content type of the request data when accessing request.data
.
By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».
Throttled
Signature: Throttled(wait=None, detail=None, code=None)
Raised when an incoming request fails the throttling checks.
By default this exception results in a response with the HTTP status code «429 Too Many Requests».
ValidationError
Signature: ValidationError(detail=None, code=None)
The ValidationError
exception is slightly different from the other APIException
classes:
- The
detail
argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in thevalidate()
method of a serializer. For example.raise serializers.ValidationError({'name': 'Please enter a valid name.'})
- By convention you should import the serializers module and use a fully qualified
ValidationError
style, in order to differentiate it from Django’s built-in validation error. For example.raise serializers.ValidationError('This field must be an integer value.')
The ValidationError
class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid
with the raise_exception
keyword argument:
serializer.is_valid(raise_exception=True)
The generic views use the raise_exception=True
flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.
By default this exception results in a response with the HTTP status code «400 Bad Request».
Generic Error Views
Django REST Framework provides two error views suitable for providing generic JSON 500
Server Error and
400
Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)
Use these as per Django’s Customizing error views documentation.
rest_framework.exceptions.server_error
Returns a response with status code 500
and application/json
content type.
Set as handler500
:
handler500 = 'rest_framework.exceptions.server_error'
rest_framework.exceptions.bad_request
Returns a response with status code 400
and application/json
content type.
Set as handler400
:
handler400 = 'rest_framework.exceptions.bad_request'
Third party packages
The following third-party packages are also available.
DRF Standardized Errors
The drf-standardized-errors package provides an exception handler that generates the same format for all 4xx and 5xx responses. It is a drop-in replacement for the default exception handler and allows customizing the error response format without rewriting the whole exception handler. The standardized error response format is easier to document and easier to handle by API consumers.
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
‒ Doug Hellmann, Python Exception Handling Techniques
Exception handling in REST framework views¶
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
The handled exceptions are:
-
Subclasses of
APIException
raised inside REST framework. -
Django’s
Http404
exception. -
Django’s
PermissionDenied
exception.
In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.
Most error responses will include a key detail
in the body of the response.
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1 Accept: application/json
Might receive an error response indicating that the DELETE
method is not allowed on that resource:
HTTP/1.1 405 Method Not Allowed Content-Type: application/json Content-Length: 42 {"detail": "Method 'DELETE' not allowed."}
Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the “non_field_errors” key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY
setting.
An example validation error might look like this:
HTTP/1.1 400 Bad Request Content-Type: application/json Content-Length: 94 {"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}
Custom exception handling¶
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.
The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function 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.
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
HTTP/1.1 405 Method Not Allowed Content-Type: application/json Content-Length: 62 {"status_code": 405, "detail": "Method 'DELETE' not allowed."}
In order to alter the style of the response, you could write the following custom exception handler:
from rest_framework.views import exception_handler def custom_exception_handler(exc, context): # Call REST framework's default exception handler first, # to get the standard error response. response = exception_handler(exc, context) # Now add the HTTP status code to the response. if response is not None: response.data['status_code'] = response.status_code return response
The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view']
.
The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER
setting key. For example:
REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler' }
If not specified, the 'EXCEPTION_HANDLER'
setting defaults to the standard exception handler provided by REST framework:
REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' }
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.
API Reference¶
APIException¶
Signature: APIException()
The base class for all exceptions raised inside an APIView
class or @api_view
.
To provide a custom exception, subclass APIException
and set the .status_code
, .default_detail
, and default_code
attributes on the class.
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:
from rest_framework.exceptions import APIException class ServiceUnavailable(APIException): status_code = 503 default_detail = 'Service temporarily unavailable, try again later.' default_code = 'service_unavailable'
Inspecting API exceptions¶
There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.
The available attributes and methods are:
-
.detail
— Return the textual description of the error. -
.get_codes()
— Return the code identifier of the error. -
.get_full_details()
— Return both the textual description and the code identifier.
In most cases the error detail will be a simple item:
>>> print(exc.detail) You do not have permission to perform this action. >>> print(exc.get_codes()) permission_denied >>> print(exc.get_full_details()) {'message':'You do not have permission to perform this action.','code':'permission_denied'}
In the case of validation errors the error detail will be either a list or
dictionary of items:
>>> print(exc.detail) {"name":"This field is required.","age":"A valid integer is required."} >>> print(exc.get_codes()) {"name":"required","age":"invalid"} >>> print(exc.get_full_details()) {"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}
ParseError¶
Signature: ParseError(detail=None, code=None)
Raised if the request contains malformed data when accessing request.data
.
By default this exception results in a response with the HTTP status code “400 Bad Request”.
AuthenticationFailed¶
Signature: AuthenticationFailed(detail=None, code=None)
Raised when an incoming request includes incorrect authentication.
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 for more details.
NotAuthenticated¶
Signature: NotAuthenticated(detail=None, code=None)
Raised when an unauthenticated request fails the permission checks.
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 for more details.
PermissionDenied¶
Signature: PermissionDenied(detail=None, code=None)
Raised when an authenticated request fails the permission checks.
By default this exception results in a response with the HTTP status code “403 Forbidden”.
NotFound¶
Signature: NotFound(detail=None, code=None)
Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404
Django exception.
By default this exception results in a response with the HTTP status code “404 Not Found”.
MethodNotAllowed¶
Signature: MethodNotAllowed(method, detail=None, code=None)
Raised when an incoming request occurs that does not map to a handler method on the view.
By default this exception results in a response with the HTTP status code “405 Method Not Allowed”.
NotAcceptable¶
Signature: NotAcceptable(detail=None, code=None)
Raised when an incoming request occurs with an Accept
header that cannot be satisfied by any of the available renderers.
By default this exception results in a response with the HTTP status code “406 Not Acceptable”.
Throttled¶
Signature: Throttled(wait=None, detail=None, code=None)
Raised when an incoming request fails the throttling checks.
By default this exception results in a response with the HTTP status code “429 Too Many Requests”.
ValidationError¶
Signature: ValidationError(detail, code=None)
The ValidationError
exception is slightly different from the other APIException
classes:
-
The
detail
argument is mandatory, not optional. -
The
detail
argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in thevalidate()
method of a serializer. For example.raise serializers.ValidationError({'name': 'Please enter a valid name.'})
-
By convention you should import the serializers module and use a fully qualified
ValidationError
style, in order to differentiate it from Django’s built-in validation error. For example.raise serializers.ValidationError('This field must be an integer value.')
The ValidationError
class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid
with the raise_exception
keyword argument:
serializer.is_valid(raise_exception=True)
The generic views use the raise_exception=True
flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.
By default this exception results in a response with the HTTP status code “400 Bad Request”.
Generic Error Views¶
Django REST Framework provides two error views suitable for providing generic JSON 500
Server Error and
400
Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)
Use these as per Django’s Customizing error views documentation.
rest_framework.exceptions.server_error
¶
Returns a response with status code 500
and application/json
content type.
Set as handler500
:
handler500 = 'rest_framework.exceptions.server_error'
rest_framework.exceptions.bad_request
¶
Returns a response with status code 400
and application/json
content type.
Set as handler400
:
handler400 = 'rest_framework.exceptions.bad_request'
Back to Top
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
— Doug Hellmann, Python Exception Handling Techniques
Exception handling in REST framework views
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
The handled exceptions are:
- Subclasses of
APIException
raised inside REST framework. - Django’s
Http404
exception. - Django’s
PermissionDenied
exception.
In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.
Most error responses will include a key detail
in the body of the response.
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1 Accept: application/json
Might receive an error response indicating that the DELETE
method is not allowed on that resource:
HTTP/1.1 405 Method Not Allowed Content-Type: application/json Content-Length: 42 {"detail": "Method 'DELETE' not allowed."}
Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY
setting.
An example validation error might look like this:
HTTP/1.1 400 Bad Request Content-Type: application/json Content-Length: 94 {"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}
Custom exception handling
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.
The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function 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.
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
HTTP/1.1 405 Method Not Allowed Content-Type: application/json Content-Length: 62 {"status_code": 405, "detail": "Method 'DELETE' not allowed."}
In order to alter the style of the response, you could write the following custom exception handler:
from rest_framework.views import exception_handler def custom_exception_handler(exc, context): response = exception_handler(exc, context) if response is not None: response.data['status_code'] = response.status_code return response
The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view']
.
The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER
setting key. For example:
REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler' }
If not specified, the 'EXCEPTION_HANDLER'
setting defaults to the standard exception handler provided by REST framework:
REST_FRAMEWORK = { 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler' }
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.
API Reference
APIException
Signature:APIException()
The base class for all exceptions raised inside an APIView
class or @api_view
.
To provide a custom exception, subclass APIException
and set the .status_code
, .default_detail
, and default_code
attributes on the class.
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:
from rest_framework.exceptions import APIException class ServiceUnavailable(APIException): status_code = 503 default_detail = 'Service temporarily unavailable, try again later.' default_code = 'service_unavailable'
Inspecting API exceptions
There are a number of different properties available for inspecting the status of an API exception. You can use these to build custom exception handling for your project.
The available attributes and methods are:
-
.detail
— Return the textual description of the error. -
.get_codes()
— Return the code identifier of the error. -
.get_full_details()
— Return both the textual description and the code identifier.
In most cases the error detail will be a simple item:
>>> print(exc.detail) You do not have permission to perform this action. >>> print(exc.get_codes()) permission_denied >>> print(exc.get_full_details()) {'message':'You do not have permission to perform this action.','code':'permission_denied'}
In the case of validation errors the error detail will be either a list or dictionary of items:
>>> print(exc.detail) {"name":"This field is required.","age":"A valid integer is required."} >>> print(exc.get_codes()) {"name":"required","age":"invalid"} >>> print(exc.get_full_details()) {"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}
ParseError
Signature:ParseError(detail=None, code=None)
Raised if the request contains malformed data when accessing request.data
.
By default this exception results in a response with the HTTP status code «400 Bad Request».
AuthenticationFailed
Signature:AuthenticationFailed(detail=None, code=None)
Raised when an incoming request includes incorrect authentication.
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 for more details.
NotAuthenticated
Signature:NotAuthenticated(detail=None, code=None)
Raised when an unauthenticated request fails the permission checks.
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 for more details.
PermissionDenied
Signature:PermissionDenied(detail=None, code=None)
Raised when an authenticated request fails the permission checks.
By default this exception results in a response with the HTTP status code «403 Forbidden».
NotFound
Signature:NotFound(detail=None, code=None)
Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404
Django exception.
By default this exception results in a response with the HTTP status code «404 Not Found».
MethodNotAllowed
Signature:MethodNotAllowed(method, detail=None, code=None)
Raised when an incoming request occurs that does not map to a handler method on the view.
By default this exception results in a response with the HTTP status code «405 Method Not Allowed».
NotAcceptable
Signature:NotAcceptable(detail=None, code=None)
Raised when an incoming request occurs with an Accept
header that cannot be satisfied by any of the available renderers.
By default this exception results in a response with the HTTP status code «406 Not Acceptable».
UnsupportedMediaType
Signature:UnsupportedMediaType(media_type, detail=None, code=None)
Raised if there are no parsers that can handle the content type of the request data when accessing request.data
.
By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».
Throttled
Signature:Throttled(wait=None, detail=None, code=None)
Raised when an incoming request fails the throttling checks.
By default this exception results in a response with the HTTP status code «429 Too Many Requests».
ValidationError
Signature:ValidationError(detail, code=None)
The ValidationError
exception is slightly different from the other APIException
classes:
- The
detail
argument is mandatory, not optional. - The
detail
argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in thevalidate()
method of a serializer. For example.raise serializers.ValidationError({'name': 'Please enter a valid name.'})
- By convention you should import the serializers module and use a fully qualified
ValidationError
style, in order to differentiate it from Django’s built-in validation error. For example.raise serializers.ValidationError('This field must be an integer value.')
The ValidationError
class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid
with the raise_exception
keyword argument:
serializer.is_valid(raise_exception=True)
The generic views use the raise_exception=True
flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.
By default this exception results in a response with the HTTP status code «400 Bad Request».
Generic Error Views
Django REST Framework provides two error views suitable for providing generic JSON 500
Server Error and 400
Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an API-only application.)
Use these as per Django’s Customizing error views documentation.
rest_framework.exceptions.server_error
Returns a response with status code 500
and application/json
content type.
Set as handler500
:
handler500 = 'rest_framework.exceptions.server_error'
rest_framework.exceptions.bad_request
Returns a response with status code 400
and application/json
content type.
Set as handler400
:
handler400 = 'rest_framework.exceptions.bad_request'
exceptions.py
Django REST Framework
3.14
-
Caching
A certain woman had very sharp consciousness but almost no memory Caching REST Framework works well with the cache utilities provided Django.
-
Content negotiation
HTTP has provisions for several mechanisms «content negotiation» the process of selecting best representation given response when there are multiple representations
-
Serializer fields
Each field in Form class is responsible not only validating data, but also «cleaning» it normalizing consistent format.
-
Custom fields
If you want to create custom field, you’ll need subclass and then override either one or both of .to_representation() .to_internal_value() methods.
-
Getting Help
-
el
-
es
-
fr
-
id
-
it
-
ja
-
ko
-
pl
-
pt-br
-
zh-hans
- Language: en
-
1.8
-
1.10
-
1.11
-
2.0
-
2.1
-
2.2
-
3.0
-
3.1
-
3.2
-
4.0
-
4.2
-
dev
-
Documentation version:
4.1
How to manage error reporting¶
When you’re running a public site you should always turn off the
DEBUG
setting. That will make your server run much faster, and will
also prevent malicious users from seeing details of your application that can be
revealed by the error pages.
However, running with DEBUG
set to False
means you’ll never see
errors generated by your site – everyone will instead see your public error
pages. You need to keep track of errors that occur in deployed sites, so Django
can be configured to create reports with details about those errors.
Email reports¶
Server errors¶
When DEBUG
is False
, Django will email the users listed in the
ADMINS
setting whenever your code raises an unhandled exception and
results in an internal server error (strictly speaking, for any response with
an HTTP status code of 500 or greater). This gives the administrators immediate
notification of any errors. The ADMINS
will get a description of the
error, a complete Python traceback, and details about the HTTP request that
caused the error.
Note
In order to send email, Django requires a few settings telling it
how to connect to your mail server. At the very least, you’ll need
to specify EMAIL_HOST
and possibly
EMAIL_HOST_USER
and EMAIL_HOST_PASSWORD
,
though other settings may be also required depending on your mail
server’s configuration. Consult the Django settings
documentation for a full list of email-related
settings.
By default, Django will send email from root@localhost. However, some mail
providers reject all email from this address. To use a different sender
address, modify the SERVER_EMAIL
setting.
To activate this behavior, put the email addresses of the recipients in the
ADMINS
setting.
404 errors¶
Django can also be configured to email errors about broken links (404 “page
not found” errors). Django sends emails about 404 errors when:
DEBUG
isFalse
;- Your
MIDDLEWARE
setting includes
django.middleware.common.BrokenLinkEmailsMiddleware
.
If those conditions are met, Django will email the users listed in the
MANAGERS
setting whenever your code raises a 404 and the request has
a referer. It doesn’t bother to email for 404s that don’t have a referer –
those are usually people typing in broken URLs or broken web bots. It also
ignores 404s when the referer is equal to the requested URL, since this
behavior is from broken web bots too.
You can tell Django to stop reporting particular 404s by tweaking the
IGNORABLE_404_URLS
setting. It should be a list of compiled
regular expression objects. For example:
import re IGNORABLE_404_URLS = [ re.compile(r'.(php|cgi)$'), re.compile(r'^/phpmyadmin/'), ]
In this example, a 404 to any URL ending with .php
or .cgi
will not be
reported. Neither will any URL starting with /phpmyadmin/
.
The following example shows how to exclude some conventional URLs that browsers and
crawlers often request:
import re IGNORABLE_404_URLS = [ re.compile(r'^/apple-touch-icon.*.png$'), re.compile(r'^/favicon.ico$'), re.compile(r'^/robots.txt$'), ]
(Note that these are regular expressions, so we put a backslash in front of
periods to escape them.)
If you’d like to customize the behavior of
django.middleware.common.BrokenLinkEmailsMiddleware
further (for
example to ignore requests coming from web crawlers), you should subclass it
and override its methods.
See also
404 errors are logged using the logging framework. By default, these log
records are ignored, but you can use them for error reporting by writing a
handler and configuring logging appropriately.
Filtering error reports¶
Warning
Filtering sensitive data is a hard problem, and it’s nearly impossible to
guarantee that sensitive data won’t leak into an error report. Therefore,
error reports should only be available to trusted team members and you
should avoid transmitting error reports unencrypted over the internet
(such as through email).
Filtering sensitive information¶
Error reports are really helpful for debugging errors, so it is generally
useful to record as much relevant information about those errors as possible.
For example, by default Django records the full traceback for the
exception raised, each traceback frame’s local variables, and the
HttpRequest
’s attributes.
However, sometimes certain types of information may be too sensitive and thus
may not be appropriate to be kept track of, for example a user’s password or
credit card number. So in addition to filtering out settings that appear to be
sensitive as described in the DEBUG
documentation, Django offers a
set of function decorators to help you control which information should be
filtered out of error reports in a production environment (that is, where
DEBUG
is set to False
): sensitive_variables()
and
sensitive_post_parameters()
.
-
sensitive_variables
(*variables)¶ -
If a function (either a view or any regular callback) in your code uses
local variables susceptible to contain sensitive information, you may
prevent the values of those variables from being included in error reports
using thesensitive_variables
decorator:from django.views.decorators.debug import sensitive_variables @sensitive_variables('user', 'pw', 'cc') def process_info(user): pw = user.pass_word cc = user.credit_card_number name = user.name ...
In the above example, the values for the
user
,pw
andcc
variables will be hidden and replaced with stars (**********
)
in the error reports, whereas the value of thename
variable will be
disclosed.To systematically hide all local variables of a function from error logs,
do not provide any argument to thesensitive_variables
decorator:@sensitive_variables() def my_function(): ...
When using multiple decorators
If the variable you want to hide is also a function argument (e.g.
‘user
’ in the following example), and if the decorated function has
multiple decorators, then make sure to place@sensitive_variables
at the top of the decorator chain. This way it will also hide the
function argument as it gets passed through the other decorators:@sensitive_variables('user', 'pw', 'cc') @some_decorator @another_decorator def process_info(user): ...
-
sensitive_post_parameters
(*parameters)¶ -
If one of your views receives an
HttpRequest
object
withPOST parameters
susceptible to
contain sensitive information, you may prevent the values of those
parameters from being included in the error reports using the
sensitive_post_parameters
decorator:from django.views.decorators.debug import sensitive_post_parameters @sensitive_post_parameters('pass_word', 'credit_card_number') def record_user_profile(request): UserProfile.create( user=request.user, password=request.POST['pass_word'], credit_card=request.POST['credit_card_number'], name=request.POST['name'], ) ...
In the above example, the values for the
pass_word
and
credit_card_number
POST parameters will be hidden and replaced with
stars (**********
) in the request’s representation inside the
error reports, whereas the value of thename
parameter will be
disclosed.To systematically hide all POST parameters of a request in error reports,
do not provide any argument to thesensitive_post_parameters
decorator:@sensitive_post_parameters() def my_view(request): ...
All POST parameters are systematically filtered out of error reports for
certaindjango.contrib.auth.views
views (login
,
password_reset_confirm
,password_change
, andadd_view
and
user_change_password
in theauth
admin) to prevent the leaking of
sensitive information such as user passwords.
Custom error reports¶
All sensitive_variables()
and sensitive_post_parameters()
do is,
respectively, annotate the decorated function with the names of sensitive
variables and annotate the HttpRequest
object with the names of sensitive
POST parameters, so that this sensitive information can later be filtered out
of reports when an error occurs. The actual filtering is done by Django’s
default error reporter filter:
django.views.debug.SafeExceptionReporterFilter
. This filter uses the
decorators’ annotations to replace the corresponding values with stars
(**********
) when the error reports are produced. If you wish to
override or customize this default behavior for your entire site, you need to
define your own filter class and tell Django to use it via the
DEFAULT_EXCEPTION_REPORTER_FILTER
setting:
DEFAULT_EXCEPTION_REPORTER_FILTER = 'path.to.your.CustomExceptionReporterFilter'
You may also control in a more granular way which filter to use within any
given view by setting the HttpRequest
’s exception_reporter_filter
attribute:
def my_view(request): if request.user.is_authenticated: request.exception_reporter_filter = CustomExceptionReporterFilter() ...
Your custom filter class needs to inherit from
django.views.debug.SafeExceptionReporterFilter
and may override the
following attributes and methods:
-
class
SafeExceptionReporterFilter
¶ -
-
cleansed_substitute
¶ -
The string value to replace sensitive value with. By default it
replaces the values of sensitive variables with stars
(**********
).
-
hidden_settings
¶ -
A compiled regular expression object used to match settings and
request.META
values considered as sensitive. By default equivalent
to:import re re.compile(r'API|TOKEN|KEY|SECRET|PASS|SIGNATURE', flags=re.IGNORECASE)
-
is_active
(request)¶ -
Returns
True
to activate the filtering in
get_post_parameters()
andget_traceback_frame_variables()
.
By default the filter is active ifDEBUG
isFalse
. Note
that sensitiverequest.META
values are always filtered along with
sensitive setting values, as described in theDEBUG
documentation.
-
get_post_parameters
(request)¶ -
Returns the filtered dictionary of POST parameters. Sensitive values
are replaced withcleansed_substitute
.
-
get_traceback_frame_variables
(request, tb_frame)¶ -
Returns the filtered dictionary of local variables for the given
traceback frame. Sensitive values are replaced with
cleansed_substitute
.
-
If you need to customize error reports beyond filtering you may specify a
custom error reporter class by defining the
DEFAULT_EXCEPTION_REPORTER
setting:
DEFAULT_EXCEPTION_REPORTER = 'path.to.your.CustomExceptionReporter'
The exception reporter is responsible for compiling the exception report data,
and formatting it as text or HTML appropriately. (The exception reporter uses
DEFAULT_EXCEPTION_REPORTER_FILTER
when preparing the exception
report data.)
Your custom reporter class needs to inherit from
django.views.debug.ExceptionReporter
.
-
class
ExceptionReporter
¶ -
-
html_template_path
¶ -
Property that returns a
pathlib.Path
representing the absolute
filesystem path to a template for rendering the HTML representation of
the exception. Defaults to the Django provided template.
-
text_template_path
¶ -
Property that returns a
pathlib.Path
representing the absolute
filesystem path to a template for rendering the plain-text
representation of the exception. Defaults to the Django provided
template.
-
get_traceback_data
()¶ -
Return a dictionary containing traceback information.
This is the main extension point for customizing exception reports, for
example:from django.views.debug import ExceptionReporter class CustomExceptionReporter(ExceptionReporter): def get_traceback_data(self): data = super().get_traceback_data() # ... remove/add something here ... return data
-
get_traceback_html
()¶ -
Return HTML version of exception report.
Used for HTML version of debug 500 HTTP error page.
-
get_traceback_text
()¶ -
Return plain text version of exception report.
Used for plain text version of debug 500 HTTP error page and email
reports.
-
As with the filter class, you may control which exception reporter class to use
within any given view by setting the HttpRequest
’s
exception_reporter_class
attribute:
def my_view(request): if request.user.is_authenticated: request.exception_reporter_class = CustomExceptionReporter() ...
See also
You can also set up custom error reporting by writing a custom piece of
exception middleware. If you do write custom
error handling, it’s a good idea to emulate Django’s built-in error handling
and only report/log errors if DEBUG
is False
.
Back to Top
exceptions.py
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
— Doug Hellmann, Python Exception Handling Techniques
Exception handling in REST framework views
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
The handled exceptions are:
- Subclasses of
APIException
raised inside REST framework. - Django’s
Http404
exception. - Django’s
PermissionDenied
exception.
In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.
By default all error responses will include a key detail
in the body of the response, but other keys may also be included.
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json
Might receive an error response indicating that the DELETE
method is not allowed on that resource:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42
{"detail": "Method 'DELETE' not allowed."}
Custom exception handling
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.
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.
For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62
{"status_code": 405, "detail": "Method 'DELETE' not allowed."}
In order to alter the style of the response, you could write the following custom exception handler:
from rest_framework.views import exception_handler
def custom_exception_handler(exc):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER
setting key. For example:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
If not specified, the 'EXCEPTION_HANDLER'
setting defaults to the standard exception handler provided by REST framework:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}
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.
API Reference
APIException
Signature: APIException()
The base class for all exceptions raised inside an APIView
class or @api_view
.
To provide a custom exception, subclass APIException
and set the .status_code
and .default_detail
properties on the class.
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:
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
ParseError
Signature: ParseError(detail=None)
Raised if the request contains malformed data when accessing request.DATA
or request.FILES
.
By default this exception results in a response with the HTTP status code «400 Bad Request».
AuthenticationFailed
Signature: AuthenticationFailed(detail=None)
Raised when an incoming request includes incorrect authentication.
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 for more details.
NotAuthenticated
Signature: NotAuthenticated(detail=None)
Raised when an unauthenticated request fails the permission checks.
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 for more details.
PermissionDenied
Signature: PermissionDenied(detail=None)
Raised when an authenticated request fails the permission checks.
By default this exception results in a response with the HTTP status code «403 Forbidden».
MethodNotAllowed
Signature: MethodNotAllowed(method, detail=None)
Raised when an incoming request occurs that does not map to a handler method on the view.
By default this exception results in a response with the HTTP status code «405 Method Not Allowed».
Signature: UnsupportedMediaType(media_type, detail=None)
Raised if there are no parsers that can handle the content type of the request data when accessing request.DATA
or request.FILES
.
By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».
Throttled
Signature: Throttled(wait=None, detail=None)
Raised when an incoming request fails the throttling checks.
By default this exception results in a response with the HTTP status code «429 Too Many Requests».
I was working on a project with a coworker recently and I noticed in one of their pull requests that they used a custom exception in one of our Django REST Framework viewsets. I prefer this way to what I was doing before, so I wanted to share it with you!
My Less Clean, Less Consistent Exception Handling
I made good use of the status codes page of the DRF docs. For example, if a user uploaded a CSV file that my view couldn’t read, I might have had something like this:
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
class MyCSVUploadView(APIView):
def post(self, request, *args, **kwargs):
…
if file_error:
return Response(
{"detail": "Unable to read file."},
status=status.HTTP_400_BAD_REQUEST
)
This works just fine. But in some cases, I might return the same type of error response in multiple places. For a university that is no longer accepting applications, I might have multiple places where I am essentially passing the same “Deadline passed” message. Although the response is just one line of code, I have to find it, copy it, and paste it to make sure I’m using the same wording, or deal with inconsistency in my exception messages.
Custom Exception Classes for Clean Consistency
For exception conditions that you encounter frequently in your code, you can override DRF’s APIException
class and create your own exceptions! Read more in the docs.
Import the APIException
class and create a new class that inherits from it. Then set the status_code
, default_detail
, and default_code
attributes.
# exceptions.py
from rest_framework.exceptions import APIException
class UnreadableCSVFile(APIException):
status_code = 400
default_detail = "Unable to read file."
default_code = "unreadable_csv_file"
The status_code
attribute defines the HTTP status code you want to send in the response with this exception; refer to the DRF list of status codes for help. The default_detail
attribute is the human-readable exception message you want to pass in the response. The default_code
is a string that represents this specific exception.
In your view, instead of manually returning a Response()
object, raise your custom exception.
# views.py
from rest_framework.views import APIView
From my_app.exceptions import UnreadableCSVFile
class MyCSVUploadView(APIView):
def post(self, request, *args, **kwargs):
…
if file_error:
raise UnreadableCSVFile()
This allows my exceptions to be reusable across my app, which means the language I use in exception messages is always consistent.
Custom Exception Handlers
You can further customize your exceptions by creating a custom handler function (docs on custom exception handling). This is especially helpful if there are pieces of data, like the status code, that you want to ensure are included in all of your HTTP responses. This is a slightly different goal than what I refer to above, but it’s worth knowing about. Here is the example from the docs:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
This adds the status_code
to each exception response sent in your app.
You would then add your custom exception handler to the REST_FRAMEWORK
settings in your settings.py
so this custom exception handler will be executed every time there is an API exception:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
For my example above, with the unreadable CSV file, the response would look like this:
{"status_code": 400, "detail": "Unable to read file."}
With the status code returned as part of the response dictionary.
Thanks to Frank Wiles for teaching me about custom exceptions, and Jeff Triplett for reviewing a draft of this article.
I was working on a project with a coworker recently and I noticed in one of their pull requests that they used a custom exception in one of our Django REST Framework viewsets. I prefer this way to what I was doing before, so I wanted to share it with you!{%
else %}
This article shows how to display an error within a form after submitting a POST request in Django. If there is an error, Django will display the detail of the error in an error message. The following is the output of a submitted form using POST request but there is a an error on that form :
The above is a form with the following directory tree project of Django framework :
(base) user@hostname:~/python/project$ tree -L 2 . ├── app │ ├── admin.py │ ├── apps.py │ ├── forms.py │ ├── __init__.py │ ├── migrations │ ├── models.py │ ├── __pycache__ │ ├── tests.py │ ├── urls.py │ └── views.py ├── db.sqlite3 ├── manage.py ├── project │ ├── __init__.py │ ├── __pycache__ │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── static ├── css ├── img └── js
The following is the content of several important files to display how to show error message. The first file is the models.py file. Below is the content of that file :
from django.db import models # Create your models here. class MyModel(models.Model): name = models.CharField(max_length=100)
The next one is the content of a file with the name of ‘forms.py’ :
from django import forms from app.models import MyModel class MyForm(forms.ModelForm): name = forms.CharField() class Meta: model = MyModel fields = ('name',)
Next, it is the ‘views.py’ file containing the method or function for processing the submit POST request from the page. The following is the content of the files :
def add(request): if request.method == "POST" or None: form = MyForm(request.POST or None) if form.is_valid(): form.save(commit=False) print(form) print("Valid Form") form.save() return render(request,'app/list.html',{'form':form}) else: print(form) print("Invalid Form") print(form.errors) return render(request, 'app/add.html',{'form':form}) else: return render(request, 'app/add.html')
The next one is the content of the template file for rendering the output. Below is a file with the name of ‘add.html’. The file itself exist inside the templates directory. The following is the content of the ‘add.html’ file :
{% extends 'app/base.html' %} {% block content %} <form method="POST" action="{% url 'app-add' %}"> {% csrf_token %} <div class="form-group"> <label for="exampleInputName">Name</label> <input type="text" class="form-control" name="name" aria-describedby="name" placeholder="Name"> </div> {% if form.errors %} {% for field in form %} {% for error in field.errors %} <div class="alert alert-danger"> <strong>{{ error|escape }}</strong> </div> {% endfor %} {% endfor %} {% endif %} <button type="submit" class="btn btn-primary">Submit</button> </form> {% endblock %}
The most important thing in the above snippet code for displaying error message is in the following tag :
{% if form.errors %} {% for field in form %} {% for error in field.errors %} <div class="alert alert-danger"> <strong>{{ error|escape }}</strong> </div> {% endfor %} {% endfor %} {% endif %}
Don’t forget to check the variable name before defining in the above error form message. It is using a variable with the name of ‘form’. It is consistent with the method definition in the view for passing the variable name to the html file page. It shows in the following line of code :
return render(request, 'app/add.html',{'form':form})
So, basically, since the form is not valid, the interpreter will execute the else block part. So, that line part above is in the execution process. Along with the execution process, a variable with the name of ‘form’ will pass as an additional data to the template page file with the name of ‘add.html’ in folder ‘app’. The ‘form’ variable itself is in the previous definition as in the following line of code :
form = MyForm(request.POST or None)
Чтобы получить больше удовольствия от чтения, приглашаем вас посетитьОригинальный блог
аномальный
Обработка исключений … Позволяет четко организовать обработку ошибок в центре или на верхнем уровне структуры программы.
Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
— Doug Hellmann, Python Exception Handling Techniques
Обработка исключений в представлении Rest framework
Exception handling in REST framework views
Представление структуры REST обрабатывает различные исключения и правильно возвращает ответ с ошибкой.
REST framework’s views handle various exceptions, and deal with returning appropriate error responses.
Обрабатываются следующие исключения:
- Брошено внутрь остального фреймворка
APIException
Подкатегории. - Джанго
Http404
аномальный. - Джанго
PermissionDenied
аномальный
В каждом случае платформа REST вернет ответ, содержащий правильный код состояния и тип содержимого. В теле ответа содержатся дополнительные сведения о самой ошибке.
Большинство ответов об ошибках будут содержатьdetail
Ключи.
Например, такой запрос:
DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json
Вы также можете получить ответ об ошибке, указывающий, что ресурсDELETE
Метод не разрешен.
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42
{"detail": "Method 'DELETE' not allowed."}
Обработка ошибок проверки немного отличается: она включает в себя имя поля в качестве ключа. Если ошибка проверки не назначена конкретному полю, она будет использоватьnon_field_errors
Как ключ, или вы задаете его в файле настроекNON_FIELD_ERRORS_KEY
Значение любой строки.
Любая ошибка проверки будет иметь следующий вид:
HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94
{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}
Пользовательская обработка исключений
Вы можете реализовать собственную обработку исключений. Вы можете преобразовать исключение, созданное в представлении API, в объект ответа, создав функцию обработки исключений. Таким образом, вы можете контролировать формат ответа на ошибку, используемый вашим API.
Эта функция обработки исключений должна передавать два параметра, первый — это исключение, которое нужно обработать, а второй — словарь, содержащий любую дополнительную контекстную информацию (например, текущее обрабатываемое представление). Функция обработки исключений либо возвращаетResponse
Объект или возврат, если исключение не может быть обработаноNone
. Если он вернетсяNone
, Исключение будет создано повторно, и, наконец, Django вернет стандартный ответ HTTP 500 «ошибка сервера».
Например, вы можете убедиться, что все тела ответов об ошибках содержат коды состояния HTTP, например:
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62
{"status_code": 405, "detail": "Method 'DELETE' not allowed."}
Чтобы изменить формат ответа, вы можете написать следующую пользовательскую функцию обработки исключений:
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
Контекст параметра не используется обработчиком исключений по умолчанию, но если вам нужна дополнительная информация, например, если вы хотите получить текущее обработанное представление, он может протянуть вам руку помощи. поcontext['view']
Вы можете получить текущий вид.
При этом вы должны настроить обработчик исключений в своих настройках и явно указатьEXCEPTION_HANDLER
Задайте желаемое значение, например:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
Если не указано,’EXCEPTION_HANDLER‘
По умолчанию используется стандартный обработчик исключений, предоставляемый платформой REST:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}
Обратите внимание, что обработчик исключений вызывается только тогда, когда ответ генерируется сгенерированным исключением. Если ответ возвращается непосредственно представлением, он не будет вызываться, напримерHTTP_400_BAD_REQUEST
Ответ возвращается универсальным представлением, когда проверка сериализации завершается неудачно, и обработчик исключений в это время не вызывается.
Справочник по API
APIException
Signature: APIException()
все вAPIView
Класс или@api_view
Выброшено исключениеБазовый класс。
Чтобы предоставить настраиваемое исключение, настраиваемый класс, унаследованный отAPIException
И установить.status_code
с участием.default_detail
Атрибуты.
Например, если ваш API использует стороннюю службу, эта служба иногда может быть недоступна. Вы можете рассмотреть возможность реализации класса исключения для кода HTTP-ответа «503 Service Unavailable». Вы можете сделать следующее:
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
ParseError
Signature: ParseError(detail=None)
В гостяхrequest.data
Если запрос содержит недопустимые данные, будет выдана ошибка.
По умолчанию исключение возвращает код состояния «400 неверный запрос».
AuthenticationFailed
Signature: AuthenticationFailed(detail=None)
Выбрасывается, когда запрос содержит неверную информацию для аутентификации.
Raised when an incoming request includes incorrect authentication.
По умолчанию исключение возвращает401 Unauthenticated
, Но также есть возможность вернуть403 Forbidden
, Это зависит от используемого режима аутентификации. Подробная ссылка на контентauthentication documentation
NotAuthenticated
Signature: NotAuthenticated(detail=None)
Вызывается, когда аутентификация неаутентифицированного запроса терпит неудачу.
По умолчанию исключение возвращает401 Unauthenticated
, Но также есть возможность вернуть403 Forbidden
, Это зависит от используемого режима аутентификации. Подробная ссылка на контентauthentication documentation
PermissionDenied
Signature: PermissionDenied(detail=None)
Вызывается, когда аутентифицированный запрос не проходит проверку разрешения.
Возврат по умолчанию403 Forbidden
NotFound
Signature: NotFound(detail=None)
Выдается, когда указанный URL не существует. Исключение эквивалентно стандартному DjangoHttp404
аномальный.
Возврат по умолчанию404 Not Found
.
MethodNotAllowed
Signature: MethodNotAllowed(method, detail=None)
Вызывается, когда в представлении нет метода обработки, соответствующего запросу.
Возврат по умолчанию405 Method Not Allowed
NotAcceptable
Signature: NotAcceptable(detail=None)
Вызывается, когда принятый заголовок запроса не удовлетворяет ни один доступный рендерер.
Возврат по умолчанию406 Not Acceptable
UnsupportedMediaType
Signature: UnsupportedMediaType(media_type, detail=None)
При посещенииrequest.data
Вызывается, когда нет парсера для обработки типа содержимого запрошенных данных.
Возврат по умолчанию415 Unsupported Media Type
Throttled
Signature: Throttled(wait=None, detail=None)
Вызывается, когда запрос превышает максимальный предел.
Возврат по умолчанию429 Too Many Requests
ValidationError
Signature: ValidationError(detail)
ValidationError
С прочимиAPIException
Классы немного разные:
The ValidationError exception is slightly different from the other APIException classes:
-
detail
Параметр является обязательным и не является обязательным. -
detail
Параметр может быть списком или словарем сведений об ошибке или встроенной структурой данных. - По соглашению вы должны импортировать модуль сериализатора и использовать полностью описанный
ValidationError
Format, чтобы отличить его от встроенных в Django ошибок проверки. Например.raise serializers.ValidationError('This field must be an integer value.')
ValidationError
Класс должен передать класс валидатора для сериализатора и проверки поля. Он также вызоветserializer.is_valid
Метод и указанныйraise_exception
Выброшен.
serializer.is_valid(raise_exception=True)
Использовать в общем видеraise_exception=True
Маркировка означает, что вы можете глобально переопределить формат ответа об ошибке проверки в вашем API. Если вы хотите сделать это, рекомендуется использовать настраиваемое исключение, описанное выше.
По умолчанию исключение возвращает400 Bad Request
Исходная ссылка