Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
  • Django Template Django Template
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 11
    • Issues 11
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • IzeniIzeni
  • Django TemplateDjango Template
  • Wiki
  • exception handling tips

exception handling tips · Changes

Page history
Update exception handling tips authored Oct 31, 2024 by Jordan Price's avatar Jordan Price
Show whitespace changes
Inline Side-by-side
exception-handling-tips.md
View page @ 8181b757
...@@ -3,6 +3,13 @@ Django REST Framework has a few quirks that aren't immediately obvious that can ...@@ -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. 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` ## 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 ### Special case of PermissionDenied and Http404
### Non DRF exceptions ### Non DRF exceptions
## Always raise an exception, never manually return a `Response` with an error status code ## Always raise an exception, never manually return a `Response` with an error status code
......
Clone repository
  • exception handling tips
  • recommended development tools
  • stripe integration