django-rest-framework/docs/api-guide/exceptions.md

88 lines
2.9 KiB
Markdown
Raw Normal View History

2012-09-09 01:06:13 +04:00
<a class="github" href="exceptions.py"></a>
2012-09-01 23:26:27 +04:00
# Exceptions
2012-09-12 13:12:13 +04:00
> Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.
>
> &mdash; Doug Hellmann, [Python Exception Handling Techniques][cite]
2012-09-01 23:26:27 +04:00
2012-09-12 13:12:13 +04:00
## Exception handling in REST framework views
2012-09-12 16:11:26 +04:00
REST framework's views handle various exceptions, and deal with returning appropriate error responses.
2012-09-12 13:12:13 +04:00
The handled exceptions are:
* Subclasses of `APIException` raised inside REST framework.
* Django's `Http404` exception.
* Django's `PermissionDenied` exception.
2012-09-12 16:11:26 +04:00
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.
2012-09-12 13:12:13 +04:00
2012-09-12 16:11:26 +04:00
By default all error responses will include a key `details` in the body of the response, but other keys may also be included.
2012-09-12 13:12:13 +04:00
For example, the following request:
DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json
Might recieve 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."}
2012-10-17 18:41:57 +04:00
---
# API Reference
2012-09-12 13:12:13 +04:00
## APIException
**Signature:** `APIException(detail=None)`
2012-10-15 16:27:50 +04:00
The **base class** for all exceptions raised inside REST framework.
2012-09-12 13:12:13 +04:00
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