diff --git a/falcon/exceptions.py b/falcon/exceptions.py index 7dc6cfe..a17ceea 100644 --- a/falcon/exceptions.py +++ b/falcon/exceptions.py @@ -150,6 +150,34 @@ class HTTPMethodNotAllowed(HTTPError): HTTPError.__init__(self, status.HTTP_405, None, **kwargs) +class HTTPNotAcceptable(HTTPError): + """406 Not Acceptable + + Use this to reject the clients without a specific media type + support in their Accept headers. + + From RFC 2616: + + "The resource identified by the request is only capable of generating + response entities which have content characteristics not acceptable + according to the accept headers sent in the request." + + """ + def __init__(self, description, **kwargs): + """Initialize + + Args: + description: Human-friendly description of the error, along with a + helpful suggestion or two. + + The remaining (optional) args are the same as for HTTPError. + + """ + + HTTPError.__init__(self, status.HTTP_406, 'Media type not acceptable', + description, **kwargs) + + class HTTPConflict(HTTPError): """409 Conflict diff --git a/falcon/tests/test_httperror.py b/falcon/tests/test_httperror.py index 42a1afd..3ddc31d 100644 --- a/falcon/tests/test_httperror.py +++ b/falcon/tests/test_httperror.py @@ -312,6 +312,8 @@ class TestHTTPError(testing.TestBase): def test_misc(self): self._misc_test(falcon.HTTPBadRequest, falcon.HTTP_400) + self._misc_test(falcon.HTTPNotAcceptable, falcon.HTTP_406, + needs_title=False) self._misc_test(falcon.HTTPConflict, falcon.HTTP_409) self._misc_test(falcon.HTTPPreconditionFailed, falcon.HTTP_412) self._misc_test(falcon.HTTPUnsupportedMediaType, falcon.HTTP_415,