Fill status code for every HTTPException

Change-Id: Ib73398f0a555c9ac4534fd3ce4a1c06e51bc5962
Closes-Bug: #1515519
This commit is contained in:
Bartlomiej Biernacki 2015-11-12 09:43:23 +01:00
parent 5aeaf3d57b
commit 11dfd95c46
2 changed files with 20 additions and 2 deletions

View File

@ -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):

View File

@ -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,