Merge "Fill status code for every HTTPException"

This commit is contained in:
Jenkins 2016-03-14 00:23:40 +00:00 committed by Gerrit Code Review
commit 090ae32542
2 changed files with 20 additions and 2 deletions

View File

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

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,