... | ... | @@ -3,6 +3,13 @@ Django REST Framework has a few quirks that aren't immediately obvious that can |
|
|
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`
|
|
|
|
|
|
The only exceptions handled by DRF's exception handler are subclasses of `rest_framework.exceptions.APIException`, which means that when explicitly raising an exception in a serializer or view, it MUST be a subclass of `APIException`. If any other type of exception is raised it will not be handled by the DRF exception handler, and will instead result in a Django error page (when DEBUG is true).
|
|
|
|
|
|
These are the subclasses of `APIException` provided by Django REST Framework. You can also create your own subclasses of `APIException` if needed. The implementation for these exceptions can be [found here](https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py)
|
|
|
|
|
|
`APIException`, `ValidationError`, `ParseError`, `AuthenticationFailed`, `NotAuthenticated`, `PermissionDenied`, `NotFound`, `MethodNotAllowed`, `NotAcceptable`, `UnsupportedMediaType`, `Throttled`
|
|
|
|
|
|
### Special case of PermissionDenied and Http404
|
|
|
### Non DRF exceptions
|
|
|
## Always raise an exception, never manually return a `Response` with an error status code
|
... | ... | |