diff options
author | Jenkins <jenkins@review.openstack.org> | 2016-03-14 00:23:40 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2016-03-14 00:23:40 +0000 |
commit | 090ae32542096246d592c9dfd53f9906883761f0 (patch) | |
tree | 72c4b771066f6493ba5dd767ee41a1bd1d383327 /heatclient | |
parent | a79156985224e1dc7f7783557101858e1acc9083 (diff) | |
parent | 11dfd95c469eea456841a9f6c7e04f34d62077c2 (diff) | |
download | python-heatclient-090ae32542096246d592c9dfd53f9906883761f0.tar.gz |
Merge "Fill status code for every HTTPException"
Diffstat (limited to 'heatclient')
-rw-r--r-- | heatclient/exc.py | 6 | ||||
-rw-r--r-- | heatclient/tests/unit/test_common_http.py | 16 |
2 files changed, 20 insertions, 2 deletions
diff --git a/heatclient/exc.py b/heatclient/exc.py index 7d4968c..9fd5bef 100644 --- a/heatclient/exc.py +++ b/heatclient/exc.py @@ -45,7 +45,7 @@ class HTTPException(BaseException): """Base exception for all HTTP-derived exceptions.""" code = 'N/A' - def __init__(self, message=None): + def __init__(self, message=None, code=None): super(HTTPException, self).__init__(message) try: self.error = jsonutils.loads(message) @@ -60,6 +60,8 @@ class HTTPException(BaseException): except Exception: self.error = {'error': {'message': self.message or self.__class__.__doc__}} + if self.code == "N/A" and code is not None: + self.code = code def __str__(self): message = self.error['error'].get('message', 'Internal Error') @@ -179,7 +181,7 @@ for obj_name in dir(sys.modules[__name__]): def from_response(response): """Return an instance of an HTTPException based on requests response.""" cls = _code_map.get(response.status_code, HTTPException) - return cls(response.content) + return cls(response.content, response.status_code) class NoTokenLookupException(Exception): diff --git a/heatclient/tests/unit/test_common_http.py b/heatclient/tests/unit/test_common_http.py index 7d14602..2b59d86 100644 --- a/heatclient/tests/unit/test_common_http.py +++ b/heatclient/tests/unit/test_common_http.py @@ -818,6 +818,22 @@ class SessionClientTest(testtools.TestCase): # Assert that the raised exception can be converted to string self.assertIsNotNone(six.text_type(e)) + def test_504_error_response(self): + # for 504 we don't have specific exception type + fake = fakes.FakeHTTPResponse( + 504, + 'FAIL', + {'content-type': 'application/octet-stream'}, + '') + self.request.return_value = (fake, '') + + client = http.SessionClient(session=mock.ANY, + auth=mock.ANY) + e = self.assertRaises(exc.HTTPException, + client.request, '', 'GET') + + self.assertEqual(504, e.code) + def test_kwargs(self): fake = fakes.FakeHTTPResponse( 200, |