diff --git a/blazarclient/exception.py b/blazarclient/exception.py index 125f18f..115133d 100644 --- a/blazarclient/exception.py +++ b/blazarclient/exception.py @@ -19,22 +19,21 @@ class BlazarClientException(Exception): """Base exception class.""" - message = _("An unknown exception occurred %s.") + message_template = _("An unknown exception occurred %s.") code = 500 def __init__(self, message=None, **kwargs): - self.kwargs = kwargs - - if 'code' not in self.kwargs: - try: - self.kwargs['code'] = self.code - except AttributeError: - pass - - if not message: - message = self.message % kwargs - - super(BlazarClientException, self).__init__(message) + if 'code' in kwargs: + self.code = kwargs['code'] + else: + # ensure code in kwargs to template message + kwargs["code"]=self.code + if message: + self.message = message + else: + self.message = self.message_template % kwargs + + super(BlazarClientException, self).__init__(self.message) class CommandError(BlazarClientException): diff --git a/blazarclient/tests/test_base.py b/blazarclient/tests/test_base.py index f04bb6b..f3384c5 100644 --- a/blazarclient/tests/test_base.py +++ b/blazarclient/tests/test_base.py @@ -126,7 +126,24 @@ def test_request_fail_without_body(self, m): kwargs = {"body": {"req_key": "req_value"}} self.assertRaises(exception.BlazarClientException, self.manager.request, url, "POST", **kwargs) + + @mock.patch('requests.request') + def test_request_fail_notfound(self, m): + + m.return_value.status_code = 404 + m.return_value.text = "{\"error_code\": 404, \"error_message\": \"Object with {'lease_id': 'aaa-bbb-ccc'} not found\", \"error_name\": 404}" + url = '/leases/aaa-bbb-ccc' + try: + resp, body = self.manager.get(url) + except exception.BlazarClientException as exc: + self.assertEqual(exc.code, 404) + print(exc) + else: + # Fail if we don't have the expected exception + # Hack because testtools doesn't support AssertRaises as context Manager + self.assertFalse(True) + m.assert_called_once() class SessionClientTestCase(tests.TestCase):