diff --git a/falcon/errors.py b/falcon/errors.py index a43317604..d16c21a9f 100644 --- a/falcon/errors.py +++ b/falcon/errors.py @@ -379,9 +379,9 @@ class HTTPNotAcceptable(HTTPError): base articles related to this error (default ``None``). """ - def __init__(self, description=None, headers=None, **kwargs): + def __init__(self, title=None, description=None, headers=None, **kwargs): super(HTTPNotAcceptable, self).__init__(status.HTTP_406, - 'Media type not acceptable', + title, description, headers, **kwargs) @@ -763,9 +763,9 @@ class HTTPUnsupportedMediaType(HTTPError): base articles related to this error (default ``None``). """ - def __init__(self, description=None, headers=None, **kwargs): + def __init__(self, title=None, description=None, headers=None, **kwargs): super(HTTPUnsupportedMediaType, self).__init__( - status.HTTP_415, 'Unsupported media type', + status.HTTP_415, title, description, headers, **kwargs) diff --git a/falcon/media/handlers.py b/falcon/media/handlers.py index 3b742e468..90e78ae20 100644 --- a/falcon/media/handlers.py +++ b/falcon/media/handlers.py @@ -47,7 +47,7 @@ def find_by_media_type(self, media_type, default): if not resolved: raise errors.HTTPUnsupportedMediaType( - '{0} is an unsupported media type.'.format(media_type) + description='{0} is an unsupported media type.'.format(media_type) ) return self.data[resolved] diff --git a/tests/test_error.py b/tests/test_error.py index 3e2b4c837..ea33ae1d2 100644 --- a/tests/test_error.py +++ b/tests/test_error.py @@ -139,6 +139,13 @@ def test_http_not_acceptable_no_title_and_desc_and_challenges(): assert e.description is None +def test_http_not_acceptable_with_title(): + try: + raise falcon.HTTPNotAcceptable(title='test title') + except falcon.HTTPNotAcceptable as e: + assert e.title == 'test title' + + def test_http_not_acceptable_with_title_and_desc_and_challenges(): try: raise falcon.HTTPNotAcceptable(description='Testdescription') @@ -153,6 +160,13 @@ def test_http_unsupported_media_type_no_title_and_desc_and_challenges(): assert e.description is None +def test_http_unsupported_media_type_with_title(): + try: + raise falcon.HTTPUnsupportedMediaType(title='test title') + except falcon.HTTPUnsupportedMediaType as e: + assert e.title == 'test title' + + def test_http_unsupported_media_type_with_title_and_desc_and_challenges(): try: raise falcon.HTTPUnsupportedMediaType(description='boom') diff --git a/tests/test_example.py b/tests/test_example.py index c2a9eed05..cbcf63818 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -82,13 +82,15 @@ class RequireJSON: def process_request(self, req, resp): if not req.client_accepts_json: raise falcon.HTTPNotAcceptable( - 'This API only supports responses encoded as JSON.', + title='406 Not Acceptable', + description='This API only supports responses encoded as JSON.', href='http://docs.examples.com/api/json') if req.method in ('POST', 'PUT'): if 'application/json' not in req.content_type: raise falcon.HTTPUnsupportedMediaType( - 'This API only supports requests encoded as JSON.', + title='415 Unsupported Media Type', + description='This API only supports requests encoded as JSON.', href='http://docs.examples.com/api/json') diff --git a/tests/test_request_media.py b/tests/test_request_media.py index 9a7d9b9b7..be277bc8a 100644 --- a/tests/test_request_media.py +++ b/tests/test_request_media.py @@ -74,8 +74,10 @@ def test_unknown_media_type(media_type): with pytest.raises(errors.HTTPUnsupportedMediaType) as err: client.resource.captured_req.media - msg = '{} is an unsupported media type.'.format(media_type) - assert err.value.description == msg + title_msg = '415 Unsupported Media Type' + description_msg = '{} is an unsupported media type.'.format(media_type) + assert err.value.title == title_msg + assert err.value.description == description_msg @pytest.mark.parametrize('media_type', [