aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework
diff options
context:
space:
mode:
authorTom Christie2012-09-01 20:26:27 +0100
committerTom Christie2012-09-01 20:26:27 +0100
commitdeedf6957d14c2808c00a009ac2c1d4528cb80c9 (patch)
treea029d40c05ceeaffa9d1fb420096c7de4878cbb5 /djangorestframework
parent02dcdca13b7cbe89e1980bab7e8274500bf9e4e1 (diff)
downloaddjango-rest-framework-deedf6957d14c2808c00a009ac2c1d4528cb80c9.tar.bz2
REST framework 2 docs
Diffstat (limited to 'djangorestframework')
-rw-r--r--djangorestframework/exceptions.py24
-rw-r--r--djangorestframework/views.py2
2 files changed, 14 insertions, 12 deletions
diff --git a/djangorestframework/exceptions.py b/djangorestframework/exceptions.py
index 315c1b1d..51c5dbb7 100644
--- a/djangorestframework/exceptions.py
+++ b/djangorestframework/exceptions.py
@@ -7,7 +7,15 @@ In addition Django's built in 403 and 404 exceptions are handled.
from djangorestframework import status
-class ParseError(Exception):
+class APIException(Exception):
+ """
+ Base class for REST framework exceptions.
+ Subclasses should provide `.status_code` and `.detail` properties.
+ """
+ pass
+
+
+class ParseError(APIException):
status_code = status.HTTP_400_BAD_REQUEST
default_detail = 'Malformed request.'
@@ -15,7 +23,7 @@ class ParseError(Exception):
self.detail = detail or self.default_detail
-class PermissionDenied(Exception):
+class PermissionDenied(APIException):
status_code = status.HTTP_403_FORBIDDEN
default_detail = 'You do not have permission to access this resource.'
@@ -23,7 +31,7 @@ class PermissionDenied(Exception):
self.detail = detail or self.default_detail
-class MethodNotAllowed(Exception):
+class MethodNotAllowed(APIException):
status_code = status.HTTP_405_METHOD_NOT_ALLOWED
default_detail = "Method '%s' not allowed."
@@ -31,7 +39,7 @@ class MethodNotAllowed(Exception):
self.detail = (detail or self.default_detail) % method
-class UnsupportedMediaType(Exception):
+class UnsupportedMediaType(APIException):
status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
default_detail = "Unsupported media type '%s' in request."
@@ -39,16 +47,10 @@ class UnsupportedMediaType(Exception):
self.detail = (detail or self.default_detail) % media_type
-class Throttled(Exception):
+class Throttled(APIException):
status_code = status.HTTP_429_TOO_MANY_REQUESTS
default_detail = "Request was throttled. Expected available in %d seconds."
def __init__(self, wait, detail=None):
import math
self.detail = (detail or self.default_detail) % int(math.ceil(wait))
-
-
-REST_FRAMEWORK_EXCEPTIONS = (
- ParseError, PermissionDenied, MethodNotAllowed,
- UnsupportedMediaType, Throttled
-)
diff --git a/djangorestframework/views.py b/djangorestframework/views.py
index a7540e0c..fa34dc9a 100644
--- a/djangorestframework/views.py
+++ b/djangorestframework/views.py
@@ -226,7 +226,7 @@ class View(DjangoView):
Handle any exception that occurs, by returning an appropriate response,
or re-raising the error.
"""
- if isinstance(exc, exceptions.REST_FRAMEWORK_EXCEPTIONS):
+ if isinstance(exc, exceptions.APIException):
return Response({'detail': exc.detail}, status=exc.status_code)
elif isinstance(exc, Http404):
return Response({'detail': 'Not found'},