From aad5eeec2ef0e417c0760866b9178f37dd76ee2f Mon Sep 17 00:00:00 2001 From: dimtruck Date: Thu, 16 Apr 2015 21:08:59 -0500 Subject: [PATCH] Add exception states when solum cli cannot connect to endpoint Added EndpointException, ConnectionError, and ConnectionRefused errors with proper error messages to provide users with more information than just "nodename nor servname provided, or not known" Change-Id: Ib1f64cd8399795375fd701b613010f93013aea3b --- .../openstack/common/apiclient/client.py | 14 ++++++++++ solumclient/tests/common/test_client.py | 27 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/solumclient/openstack/common/apiclient/client.py b/solumclient/openstack/common/apiclient/client.py index 33e151b..82d56b6 100644 --- a/solumclient/openstack/common/apiclient/client.py +++ b/solumclient/openstack/common/apiclient/client.py @@ -242,6 +242,20 @@ class HTTPClient(object): try: return self.request( method, self.concat_url(endpoint, url), **kwargs) + except exceptions.ConnectionRefused as refused_ex: + print('ERROR: Cannot connect to %s. Make sure it ' + + 'is valid: %s.' % endpoint) + raise refused_ex + except exceptions.EndpointException as endpoint_ex: + print('ERROR: Service catalog endpoint is invalid. ' + + 'Please check your ' + + 'endpoint is valid: %s.' % endpoint) + raise endpoint_ex + except requests.ConnectionError as conn_err: + print('ERROR: Unable to connect to endpoint. ' + + 'Make sure that ' + + 'your endpoint (%s) is valid.' % endpoint) + raise conn_err except exceptions.Unauthorized as unauth_ex: if just_authenticated: raise diff --git a/solumclient/tests/common/test_client.py b/solumclient/tests/common/test_client.py index ddf73fb..e6f6a25 100644 --- a/solumclient/tests/common/test_client.py +++ b/solumclient/tests/common/test_client.py @@ -67,3 +67,30 @@ class ClientTest(base.TestCase): self.assertRaises( exceptions.HttpError, http_client.client_request, TestClient(http_client), "GET", "/resource") + + def test_client_with_invalid_endpoint(self): + http_client = client.HTTPClient(FakeAuthPlugin()) + mock_request = mock.Mock() + mock_request.side_effect = requests.ConnectionError + with mock.patch("requests.Session.request", mock_request): + self.assertRaises( + requests.ConnectionError, http_client.client_request, + TestClient(http_client), "GET", "/resource") + + def test_client_with_invalid_service_catalog(self): + http_client = client.HTTPClient(FakeAuthPlugin()) + mock_request = mock.Mock() + mock_request.side_effect = exceptions.EndpointException + with mock.patch("requests.Session.request", mock_request): + self.assertRaises( + exceptions.EndpointException, http_client.client_request, + TestClient(http_client), "GET", "/resource") + + def test_client_with_connection_refused(self): + http_client = client.HTTPClient(FakeAuthPlugin()) + mock_request = mock.Mock() + mock_request.side_effect = exceptions.ConnectionRefused + with mock.patch("requests.Session.request", mock_request): + self.assertRaises( + exceptions.ConnectionRefused, http_client.client_request, + TestClient(http_client), "GET", "/resource")