Skip to content

Commit

Permalink
Adding more descriptive exception messages to client
Browse files Browse the repository at this point in the history
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:
hpe-storage/python-3parclient#9

Change-Id: I7fe700ae11703dabd82436a105f634ddd5e8b8b1
  • Loading branch information
leeantho committed Mar 3, 2015
1 parent 87f68a2 commit fa50ded
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion hplefthandclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"""

version_tuple = (1, 0, 4)
version_tuple = (1, 0, 5)


def get_version_string():
Expand Down
23 changes: 19 additions & 4 deletions hplefthandclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@
# 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
Expand All @@ -67,8 +69,21 @@ def login(self, username, password):
: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):
"""
Expand Down
24 changes: 19 additions & 5 deletions hplefthandclient/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -76,6 +87,9 @@ def get_code(self):
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:
Expand Down

0 comments on commit fa50ded

Please sign in to comment.