From 11dfd95c469eea456841a9f6c7e04f34d62077c2 Mon Sep 17 00:00:00 2001 From: Bartlomiej Biernacki Date: Thu, 12 Nov 2015 09:43:23 +0100 Subject: [PATCH] Fill status code for every HTTPException Change-Id: Ib73398f0a555c9ac4534fd3ce4a1c06e51bc5962 Closes-Bug: #1515519 --- heatclient/exc.py | 6 ++++-- 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 4f8a0623..d42ca0dd 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 bb2fa20b..13c6e221 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,