diff --git a/.travis.yml b/.travis.yml index adb5efdf..3650107d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,10 +19,13 @@ matrix: env: TOXENV=pypy3 - os: osx language: generic - env: TOXENV=py34 + env: TOXENV=py35 - os: osx language: generic - env: TOXENV=py35 + env: TOXENV=py36 + - os: osx + language: generic + env: TOXENV=py37 before_install: - "./scripts/before_install.sh" install: diff --git a/CHANGELOG.md b/CHANGELOG.md index 8715080a..5a9ed598 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ Ideally, within a virtual environment. Changelog ========= +### 2.4.8 - TBD +- Fixed issue #762 - HTTP errors crash with selectable output types + ### 2.4.7 - March 28, 2019 - Fixed API documentation with selectable output types diff --git a/hug/api.py b/hug/api.py index 90f90321..22367954 100644 --- a/hug/api.py +++ b/hug/api.py @@ -369,7 +369,8 @@ def server(self, default_not_found=True, base_url=None): def error_serializer(request, response, error): response.content_type = self.output_format.content_type - response.body = self.output_format({"errors": {error.title: error.description}}) + response.body = self.output_format({"errors": {error.title: error.description}}, + request, response) falcon_api.set_error_serializer(error_serializer) return falcon_api diff --git a/scripts/before_install.sh b/scripts/before_install.sh index 85c818dd..51d88bb3 100755 --- a/scripts/before_install.sh +++ b/scripts/before_install.sh @@ -1,4 +1,4 @@ -#! /bin/bash + #! /bin/bash echo $TRAVIS_OS_NAME @@ -16,6 +16,10 @@ echo $TRAVIS_OS_NAME python_minor=4;; py35) python_minor=5;; + py36) + python_minor=6;; + py37) + python_minor=7;; esac latest_version=`pyenv install --list | grep -e "^[ ]*3\.$python_minor" | tail -1` diff --git a/tests/test_output_format.py b/tests/test_output_format.py index 870e8384..d9fe3455 100644 --- a/tests/test_output_format.py +++ b/tests/test_output_format.py @@ -268,6 +268,26 @@ class FakeRequest(object): assert formatter('hi', request, response) == b'"hi"' +def test_accept_with_http_errors(): + """Ensure that content type based output formats work for HTTP error responses""" + formatter = hug.output_format.accept({'application/json': hug.output_format.json, + 'text/plain': hug.output_format.text}, + default=hug.output_format.json) + + api = hug.API('test_accept_with_http_errors') + hug.default_output_format(api=api)(formatter) + + @hug.get('/500', api=api) + def error_500(): + raise hug.HTTPInternalServerError('500 Internal Server Error', + 'This is an example') + + response = hug.test.get(api, '/500') + assert response.status == '500 Internal Server Error' + assert response.data == { + 'errors': {'500 Internal Server Error': 'This is an example'}} + + def test_suffix(): """Ensure that it's possible to route the output type format by the suffix of the requested URL""" formatter = hug.output_format.suffix({'.js': hug.output_format.json, '.html': hug.output_format.text})