aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--djangorestframework/exceptions.py16
-rw-r--r--djangorestframework/request.py6
-rw-r--r--djangorestframework/views.py5
3 files changed, 15 insertions, 12 deletions
diff --git a/djangorestframework/exceptions.py b/djangorestframework/exceptions.py
index b29d96ba..3f7e9029 100644
--- a/djangorestframework/exceptions.py
+++ b/djangorestframework/exceptions.py
@@ -17,12 +17,20 @@ class PermissionDenied(Exception):
self.detail = detail or self.default_detail
+class MethodNotAllowed(Exception):
+ status_code = status.HTTP_405_METHOD_NOT_ALLOWED
+ default_detail = "Method '%s' not allowed"
+
+ def __init__(self, method, detail):
+ self.detail = (detail or self.default_detail) % method
+
+
class UnsupportedMediaType(Exception):
- status_code = 415
- default_detail = 'Unsupported media type in request'
+ status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE
+ default_detail = "Unsupported media type '%s' in request"
- def __init__(self, detail=None):
- self.detail = detail or self.default_detail
+ def __init__(self, media_type, detail=None):
+ self.detail = (detail or self.default_detail) % media_type
# class Throttled(Exception):
# def __init__(self, detail):
diff --git a/djangorestframework/request.py b/djangorestframework/request.py
index 2c0f0319..684f6591 100644
--- a/djangorestframework/request.py
+++ b/djangorestframework/request.py
@@ -207,8 +207,7 @@ class Request(object):
"""
Parse the request content.
- May raise a 415 ImmediateResponse (Unsupported Media Type), or a
- 400 ImmediateResponse (Bad Request).
+ May raise an `UnsupportedMediaType`, or `ParseError` exception.
"""
if self.stream is None or self.content_type is None:
return (None, None)
@@ -217,8 +216,7 @@ class Request(object):
if parser.can_handle_request(self.content_type):
return parser.parse(self.stream, self.META, self.upload_handlers)
- raise UnsupportedMediaType("Unsupported media type in request '%s'" %
- self._content_type)
+ raise UnsupportedMediaType(self._content_type)
def _authenticate(self):
"""
diff --git a/djangorestframework/views.py b/djangorestframework/views.py
index b0e23534..36d05721 100644
--- a/djangorestframework/views.py
+++ b/djangorestframework/views.py
@@ -147,10 +147,7 @@ class View(DjangoView):
Return an HTTP 405 error if an operation is called which does not have
a handler method.
"""
- content = {
- 'detail': "Method '%s' not allowed." % request.method
- }
- raise ImmediateResponse(content, status.HTTP_405_METHOD_NOT_ALLOWED)
+ raise exceptions.MethodNotAllowed(request.method)
@property
def _parsed_media_types(self):