|
|
Django REST Framework has a few quirks that aren't immediately obvious that can make handling errors from a frontend challenging if certain rules are not followed. Following these rules when raising exceptions in DRF views and serializers allow the frontend to handle error responses easily and in a consistent manner.
|
|
|
|
|
|
Before continuing, please read the [Exceptions](https://www.django-rest-framework.org/api-guide/exceptions/) API Guide page on DRF's site.
|
|
|
|
|
|
## Serializers and views must only raise subclasses of `rest_framework.exceptions.APIException`
|
|
|
### Special case of PermissionDenied and Http404
|
|
|
### Non DRF exceptions
|
|
|
## Always raise an exception, never manually return a `Response` with an error status code
|
|
|
## `ValidationError` must only be raised in the `validate` or `validate_<field>` methods of a serializer
|
|
|
## Messages will be displayed to the user! |
|
|
\ No newline at end of file |