diff options
| author | Tom Christie | 2012-10-30 14:32:31 +0000 |
|---|---|---|
| committer | Tom Christie | 2012-10-30 14:32:31 +0000 |
| commit | 9b30dab4f772f67a626e176dc4fae0a3ef9c2c81 (patch) | |
| tree | ca138abf4792f58ffa28684f784f201ee1eef6d7 /docs/api-guide/exceptions.md | |
| parent | 7e5b1501b5cede61a9391fb1a751d2ebcdb37031 (diff) | |
| parent | 4e7805cb24d73e7f706318b5e5a27e3f9ba39d14 (diff) | |
| download | django-rest-framework-9b30dab4f772f67a626e176dc4fae0a3ef9c2c81.tar.bz2 | |
Merge branch 'restframework2' into rest-framework-2-merge2.0.0
Conflicts:
.gitignore
.travis.yml
AUTHORS
README.rst
djangorestframework/mixins.py
djangorestframework/renderers.py
djangorestframework/resources.py
djangorestframework/serializer.py
djangorestframework/templates/djangorestframework/base.html
djangorestframework/templates/djangorestframework/login.html
djangorestframework/templatetags/add_query_param.py
djangorestframework/tests/accept.py
djangorestframework/tests/authentication.py
djangorestframework/tests/content.py
djangorestframework/tests/reverse.py
djangorestframework/tests/serializer.py
djangorestframework/views.py
docs/examples.rst
docs/examples/blogpost.rst
docs/examples/modelviews.rst
docs/examples/objectstore.rst
docs/examples/permissions.rst
docs/examples/pygments.rst
docs/examples/views.rst
docs/howto/alternativeframeworks.rst
docs/howto/mixin.rst
docs/howto/reverse.rst
docs/howto/usingurllib2.rst
docs/index.rst
docs/topics/release-notes.md
examples/sandbox/views.py
rest_framework/__init__.py
rest_framework/compat.py
rest_framework/utils/breadcrumbs.py
setup.py
Diffstat (limited to 'docs/api-guide/exceptions.md')
| -rw-r--r-- | docs/api-guide/exceptions.md | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/docs/api-guide/exceptions.md b/docs/api-guide/exceptions.md new file mode 100644 index 00000000..ba57fde8 --- /dev/null +++ b/docs/api-guide/exceptions.md @@ -0,0 +1,88 @@ +<a class="github" href="exceptions.py"></a> + +# Exceptions + +> 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][cite] + +## 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 `details` 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; charset=utf-8 + Content-Length: 42 + + {"detail": "Method 'DELETE' not allowed."} + +--- + +# API Reference + +## APIException + +**Signature:** `APIException(detail=None)` + +The **base class** for all exceptions raised inside REST framework. + +To provide a custom exception, subclass `APIException` and set the `.status_code` and `.detail` properties on the class. + +## 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". + +## PermissionDenied + +**Signature:** `PermissionDenied(detail=None)` + +Raised when an incoming 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". + +## UnsupportedMediaType + +**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". + +[cite]: http://www.doughellmann.com/articles/how-tos/python-exception-handling/index.html |
