From fa50dedddce3a566ab9f5493d830cd5e6a71ab4d Mon Sep 17 00:00:00 2001 From: Anthony Lee Date: Fri, 27 Feb 2015 10:12:00 -0800 Subject: [PATCH] Adding more descriptive exception messages to client Errors occuring during login were not very descriptive. This patch adds more details about why a login to a LH backend failed. Also, added the ability to toggle debug on before the client has been initialized. bug reference: https://github.com/hp-storage/python-3parclient/issues/9 Change-Id: I7fe700ae11703dabd82436a105f634ddd5e8b8b1 --- docs/changelog.rst | 6 ++++++ hplefthandclient/__init__.py | 2 +- hplefthandclient/client.py | 23 +++++++++++++++++++---- hplefthandclient/exceptions.py | 24 +++++++++++++++++++----- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 66c86d4..aeb47ed 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -51,3 +51,9 @@ Changes in Version 1.0.4 * Added tox environments to run tests with code coverage and to generate the documentation * Consolidated the test/README.rst into the top level README.rst and added clarifications * Added the ability for getVolumes to filter based on cluster and fields. + +Changes in Version 1.0.5 +------------------------ + +* Added improved error handling during login attempts. Errors will now be + more descriptive in why a login failed. diff --git a/hplefthandclient/__init__.py b/hplefthandclient/__init__.py index 0377680..002a863 100644 --- a/hplefthandclient/__init__.py +++ b/hplefthandclient/__init__.py @@ -24,7 +24,7 @@ HP LeftHand REST Client """ -version_tuple = (1, 0, 4) +version_tuple = (1, 0, 5) def get_version_string(): diff --git a/hplefthandclient/client.py b/hplefthandclient/client.py index 5d9e3c1..fb16642 100644 --- a/hplefthandclient/client.py +++ b/hplefthandclient/client.py @@ -35,16 +35,18 @@ except ImportError: # Fall back to Python 2's urllib2 from urllib2 import quote -from hplefthandclient import http +from hplefthandclient import exceptions, http class HPLeftHandClient: - def __init__(self, api_url): + def __init__(self, api_url, debug=False): self.api_url = api_url self.http = http.HTTPJSONRESTClient(self.api_url) self.api_version = None + self.debug_rest(debug) + def debug_rest(self, flag): """ This is useful for debugging requests to LeftHand @@ -67,8 +69,21 @@ class HPLeftHandClient: :returns: None """ - resp = self.http.authenticate(username, password) - self.api_version = resp['x-api-version'] + try: + resp = self.http.authenticate(username, password) + self.api_version = resp['x-api-version'] + except Exception as ex: + ex_desc = ex.get_description() + + if (ex_desc and "Unable to find the server at" in ex_desc or + "Only absolute URIs are allowed" in ex_desc): + raise exceptions.HTTPBadRequest(ex_desc) + else: + msg = ('Error: \'%s\' - Error communicating with the LeftHand ' + 'API. Check proxy settings. If error persists, either ' + 'the LeftHand API is not running or the version of the ' + 'API is not supported.') % ex_desc + raise exceptions.UnsupportedVersion(msg) def logout(self): """ diff --git a/hplefthandclient/exceptions.py b/hplefthandclient/exceptions.py index 3bb9795..796bb31 100644 --- a/hplefthandclient/exceptions.py +++ b/hplefthandclient/exceptions.py @@ -54,16 +54,27 @@ class ClientException(Exception): """ _error_code = None _error_desc = None + _error_ref = None _debug1 = None _debug2 = None def __init__(self, error=None): - if error: - if 'messageID' in error: - self._error_code = error['messageID'] - if 'message' in error: - self._error_desc = error['message'] + super(ClientException, self).__init__() + + if not error: + return + + if isinstance(error, str): + # instead of KeyError below, take it and make it the _error_desc. + self._error_desc = error + else: + if 'code' in error: + self._error_code = error['code'] + if 'desc' in error: + self._error_desc = error['desc'] + if 'ref' in error: + self._error_ref = error['ref'] if 'debug1' in error: self._debug1 = error['debug1'] @@ -76,6 +87,9 @@ class ClientException(Exception): def get_description(self): return self._error_desc + def get_ref(self): + return self._error_ref + def __str__(self): formatted_string = "%s (HTTP %s)" % (self.message, self.http_status) if self._error_code: