Raise a specific exception instead of generic AttributeError

Currently we raise an AttributeError when expected attribute is missing
from the response. This is not very developer-friendly, so this patch
creates a new exception (derived from AttributeError for compatibility)
for this purpose.

Change-Id: If2ac03dcd410d451dc7bbfe7bbd0b145da201c9a
Related-Bug: #1550328
This commit is contained in:
Dmitry Tantsur 2016-11-07 15:47:44 +01:00
parent ba1eb4aa99
commit aeebdf890b
3 changed files with 11 additions and 6 deletions

View File

@ -39,6 +39,10 @@ class DRACEmptyResponseField(BaseClientException):
msg_fmt = ("Attribute '%(attr)s' is not nullable, but no value received")
class DRACMissingResponseField(BaseClientException, AttributeError):
msg_fmt = ("Attribute '%(attr)s' is missing from the response")
class InvalidParameterValue(BaseClientException):
msg_fmt = '%(reason)s'

View File

@ -40,7 +40,8 @@ class UtilsTestCase(base.BaseTest):
self.assertEqual('1', val)
def test_get_wsman_resource_attr_missing_attr(self):
expected_message = ("Could not find attribute 'HyperThreadingEnabled'")
expected_message = ("Attribute 'HyperThreadingEnabled' is missing "
"from the response")
doc = etree.fromstring(
test_utils.InventoryEnumerations[
uris.DCIM_CPUView]['missing_flags'])
@ -48,7 +49,7 @@ class UtilsTestCase(base.BaseTest):
find_all=True)
self.assertRaisesRegexp(
AttributeError, re.escape(expected_message),
exceptions.DRACMissingResponseField, re.escape(expected_message),
utils.get_wsman_resource_attr, cpus[0], uris.DCIM_CPUView,
'HyperThreadingEnabled', allow_missing=False)

View File

@ -58,9 +58,9 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
value of True. In this case, it will return None.
:param allow_missing: if set to True, attributes missing from the XML
document will return None instead of raising
AttributeError.
:raises: AttributeError if the attribute is missing from the XML doc and
allow_missing is False.
DRACMissingResponseField.
:raises: DRACMissingResponseField if the attribute is missing from the XML
doc and allow_missing is False.
:raises: DRACEmptyResponseField if the attribute is present in the XML doc
but it has no text and nullable is False.
:returns: value of the attribute
@ -71,7 +71,7 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
if allow_missing:
return
else:
raise AttributeError("Could not find attribute '%s'" % (attr_name))
raise exceptions.DRACMissingResponseField(attr=attr_name)
if not nullable:
if item.text is None: