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:
parent
ba1eb4aa99
commit
aeebdf890b
@ -39,6 +39,10 @@ class DRACEmptyResponseField(BaseClientException):
|
|||||||
msg_fmt = ("Attribute '%(attr)s' is not nullable, but no value received")
|
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):
|
class InvalidParameterValue(BaseClientException):
|
||||||
msg_fmt = '%(reason)s'
|
msg_fmt = '%(reason)s'
|
||||||
|
|
||||||
|
@ -40,7 +40,8 @@ class UtilsTestCase(base.BaseTest):
|
|||||||
self.assertEqual('1', val)
|
self.assertEqual('1', val)
|
||||||
|
|
||||||
def test_get_wsman_resource_attr_missing_attr(self):
|
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(
|
doc = etree.fromstring(
|
||||||
test_utils.InventoryEnumerations[
|
test_utils.InventoryEnumerations[
|
||||||
uris.DCIM_CPUView]['missing_flags'])
|
uris.DCIM_CPUView]['missing_flags'])
|
||||||
@ -48,7 +49,7 @@ class UtilsTestCase(base.BaseTest):
|
|||||||
find_all=True)
|
find_all=True)
|
||||||
|
|
||||||
self.assertRaisesRegexp(
|
self.assertRaisesRegexp(
|
||||||
AttributeError, re.escape(expected_message),
|
exceptions.DRACMissingResponseField, re.escape(expected_message),
|
||||||
utils.get_wsman_resource_attr, cpus[0], uris.DCIM_CPUView,
|
utils.get_wsman_resource_attr, cpus[0], uris.DCIM_CPUView,
|
||||||
'HyperThreadingEnabled', allow_missing=False)
|
'HyperThreadingEnabled', allow_missing=False)
|
||||||
|
|
||||||
|
@ -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.
|
value of True. In this case, it will return None.
|
||||||
:param allow_missing: if set to True, attributes missing from the XML
|
:param allow_missing: if set to True, attributes missing from the XML
|
||||||
document will return None instead of raising
|
document will return None instead of raising
|
||||||
AttributeError.
|
DRACMissingResponseField.
|
||||||
:raises: AttributeError if the attribute is missing from the XML doc and
|
:raises: DRACMissingResponseField if the attribute is missing from the XML
|
||||||
allow_missing is False.
|
doc and allow_missing is False.
|
||||||
:raises: DRACEmptyResponseField if the attribute is present in the XML doc
|
:raises: DRACEmptyResponseField if the attribute is present in the XML doc
|
||||||
but it has no text and nullable is False.
|
but it has no text and nullable is False.
|
||||||
:returns: value of the attribute
|
:returns: value of the attribute
|
||||||
@ -71,7 +71,7 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
|
|||||||
if allow_missing:
|
if allow_missing:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
raise AttributeError("Could not find attribute '%s'" % (attr_name))
|
raise exceptions.DRACMissingResponseField(attr=attr_name)
|
||||||
|
|
||||||
if not nullable:
|
if not nullable:
|
||||||
if item.text is None:
|
if item.text is None:
|
||||||
|
Loading…
Reference in New Issue
Block a user