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
This commit is contained in:
Anthony Lee 2015-02-27 10:12:00 -08:00
parent 87f68a2ca1
commit fa50dedddc
4 changed files with 45 additions and 10 deletions

View File

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

View File

@ -24,7 +24,7 @@ HP LeftHand REST Client
"""
version_tuple = (1, 0, 4)
version_tuple = (1, 0, 5)
def get_version_string():

View File

@ -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):
"""

View File

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