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: <attribute 'message' of 'exceptions.BaseException' objects>

Setting a message for ClientException will ensure that something more
useful than that is printed.

Change-Id: I43a2a33017f9a5c1b79d7fd8af4153e91d296f7b
Closes-bug: 1295293
This commit is contained in:
Johannes Erdfelt 2014-03-20 11:11:12 -07:00
parent 8c7524b8bf
commit e43825bd1c
2 changed files with 22 additions and 0 deletions

View File

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

View File

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