From e43825bd1c67d22f012978c5de6feff028c75d40 Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Thu, 20 Mar 2014 11:11:12 -0700 Subject: [PATCH] Print a useful message for unknown server errors If a server error is returned that has an unknown code, novaclient will end up printing something similar to: ClientException: Setting a message for ClientException will ensure that something more useful than that is printed. Change-Id: I43a2a33017f9a5c1b79d7fd8af4153e91d296f7b Closes-bug: 1295293 --- novaclient/exceptions.py | 2 ++ novaclient/tests/test_http.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/novaclient/exceptions.py b/novaclient/exceptions.py index 9f22a9e3e..a987c562c 100644 --- a/novaclient/exceptions.py +++ b/novaclient/exceptions.py @@ -81,6 +81,8 @@ class ClientException(Exception): """ The base exception class for all exceptions this library raises. """ + message = 'Unknown Error' + def __init__(self, code, message=None, details=None, request_id=None, url=None, method=None): self.code = code diff --git a/novaclient/tests/test_http.py b/novaclient/tests/test_http.py index e2fc4fa10..e7092781d 100644 --- a/novaclient/tests/test_http.py +++ b/novaclient/tests/test_http.py @@ -13,6 +13,7 @@ import mock import requests +import six from novaclient import client from novaclient import exceptions @@ -37,6 +38,12 @@ bad_req_response = utils.TestResponse({ }) bad_req_mock_request = mock.Mock(return_value=(bad_req_response)) +unknown_error_response = utils.TestResponse({ + "status_code": 503, + "text": '', +}) +unknown_error_mock_request = mock.Mock(return_value=unknown_error_response) + def get_client(): cl = client.HTTPClient("username", "password", @@ -133,3 +140,16 @@ class ClientTest(utils.TestCase): cl2 = client.HTTPClient("username", "password", "project_id", "auth_test", http_log_debug=True) self.assertEqual(len(cl2._logger.handlers), 1) + + @mock.patch.object(requests.Session, 'request', unknown_error_mock_request) + def test_unknown_server_error(self): + cl = get_client() + # This would be cleaner with the context manager version of + # assertRaises or assertRaisesRegexp, but both only appeared in + # Python 2.7 and testtools doesn't match that implementation yet + try: + cl.get('/hi') + except exceptions.ClientException as exc: + self.assertIn('Unknown Error', six.text_type(exc)) + else: + self.fail('Expected exceptions.ClientException')