diff options
author | Bartlomiej Biernacki <pax0r@o2.pl> | 2015-11-12 09:43:23 +0100 |
---|---|---|
committer | Bartlomiej Biernacki <pax0r@o2.pl> | 2015-11-12 09:43:23 +0100 |
commit | 11dfd95c469eea456841a9f6c7e04f34d62077c2 (patch) | |
tree | 5deddc3e556d6a6ee0844009d592595fdc92e944 /heatclient | |
parent | 5aeaf3d57bf8dae3acefe605486b6d8afaeed738 (diff) | |
download | python-heatclient-11dfd95c469eea456841a9f6c7e04f34d62077c2.tar.gz |
Fill status code for every HTTPException
Change-Id: Ib73398f0a555c9ac4534fd3ce4a1c06e51bc5962
Closes-Bug: #1515519
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 4f8a062..d42ca0d 100644 --- a/heatclient/exc.py +++ b/heatclient/exc.py @@ -44,7 +44,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) @@ -59,6 +59,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 bb2fa20..13c6e22 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, |