diff --git a/blazarclient/base.py b/blazarclient/base.py index 1a095eb..025729c 100644 --- a/blazarclient/base.py +++ b/blazarclient/base.py @@ -113,7 +113,10 @@ def request(self, url, method, **kwargs): error_message = resp.text body = _("ERROR: {0}").format(error_message) - raise exception.BlazarClientException(body, code=resp.status_code) + if resp.status_code == 404: + raise exception.NotFound(body, code=resp.status_code) + else: + raise exception.BlazarClientException(body, code=resp.status_code) return resp, body @@ -132,7 +135,11 @@ def request(self, url, method, **kwargs): error_message = resp.text msg = _("ERROR: {0}").format(error_message) - raise exception.BlazarClientException(msg, code=resp.status_code) + + if resp.status_code == 404: + raise exception.NotFound(msg, code=resp.status_code) + else: + raise exception.BlazarClientException(msg, code=resp.status_code) return resp, body diff --git a/blazarclient/exception.py b/blazarclient/exception.py index 125f18f..102f6ae 100644 --- a/blazarclient/exception.py +++ b/blazarclient/exception.py @@ -98,7 +98,19 @@ class IncorrectNetwork(BlazarClientException): code = 409 -class ResourcePropertyNotFound(BlazarClientException): +class NotFound(BlazarClientException): + """HTTP 404 - Not Found. + + The requested resource could not be found but may be available again + in the future. + """ + message = _("Not Found") + code = 404 + +class ResourcePropertyNotFound(NotFound): """Occurs if the resource property specified does not exist""" message = _("The resource property does not exist.") - code = 404 + +class ResourceNotFound(NotFound): + """Occurs if specified resource does not exist""" + message = _("The resource does not exist.") \ No newline at end of file diff --git a/blazarclient/tests/test_base.py b/blazarclient/tests/test_base.py index f04bb6b..fa5c1d7 100644 --- a/blazarclient/tests/test_base.py +++ b/blazarclient/tests/test_base.py @@ -126,6 +126,14 @@ 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 = "resp" + url = '/leases' + self.assertRaises(exception.NotFound, + self.manager.request, url, "GET") class SessionClientTestCase(tests.TestCase): @@ -156,6 +164,20 @@ def test_request_fail(self, m): kwargs = {"body": {"req_key": "req_value"}} self.assertRaises(exception.BlazarClientException, self.manager.request, url, "POST", **kwargs) + + @mock.patch('blazarclient.base.adapter.LegacyJsonAdapter.request') + def test_request_fail_notfound(self, m): + resp = mock.Mock() + resp.status_code = 404 + body = {"error message": "error"} + m.return_value = (resp, body) + url = '/leases' + kwargs = {"body": {"req_key": "req_value"}} + self.assertRaises(exception.NotFound, + self.manager.request, url, "POST", **kwargs) + + + class BaseClientManagerTestCase(tests.TestCase): @@ -188,3 +210,5 @@ def test_init_with_insufficient_info(self): blazar_url=None, auth_token=self.auth_token, session=None) + + \ No newline at end of file diff --git a/blazarclient/utils.py b/blazarclient/utils.py index 1019b99..5955608 100644 --- a/blazarclient/utils.py +++ b/blazarclient/utils.py @@ -137,7 +137,7 @@ def _find_resource_id_by_name(client, resource_type, name, name_key): return named_resources[0] else: message = "Unable to find resource with name '%s'" % name - raise exception.BlazarClientException(message=message, + raise exception.ResourceNotFound(message=message, status_code=404)