diff --git a/djangorestframework/exceptions.py b/djangorestframework/exceptions.py index b29d96ba1..3f7e9029c 100644 --- a/djangorestframework/exceptions.py +++ b/djangorestframework/exceptions.py @@ -17,12 +17,20 @@ class PermissionDenied(Exception): self.detail = detail or self.default_detail -class UnsupportedMediaType(Exception): - status_code = 415 - default_detail = 'Unsupported media type in request' +class MethodNotAllowed(Exception): + status_code = status.HTTP_405_METHOD_NOT_ALLOWED + default_detail = "Method '%s' not allowed" - def __init__(self, detail=None): - self.detail = detail or self.default_detail + def __init__(self, method, detail): + self.detail = (detail or self.default_detail) % method + + +class UnsupportedMediaType(Exception): + status_code = status.HTTP_415_UNSUPPORTED_MEDIA_TYPE + default_detail = "Unsupported media type '%s' in request" + + 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 2c0f03193..684f65914 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 b0e235340..36d05721c 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):