From aeebdf890bfc25d42a8df8958f36c3347252c0a8 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 7 Nov 2016 15:47:44 +0100 Subject: [PATCH] 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 --- dracclient/exceptions.py | 4 ++++ dracclient/tests/test_utils.py | 5 +++-- dracclient/utils.py | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/dracclient/exceptions.py b/dracclient/exceptions.py index 2982930..13bd261 100644 --- a/dracclient/exceptions.py +++ b/dracclient/exceptions.py @@ -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' diff --git a/dracclient/tests/test_utils.py b/dracclient/tests/test_utils.py index a5e71f7..e99db8b 100644 --- a/dracclient/tests/test_utils.py +++ b/dracclient/tests/test_utils.py @@ -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) diff --git a/dracclient/utils.py b/dracclient/utils.py index 4f503da..935cdb4 100644 --- a/dracclient/utils.py +++ b/dracclient/utils.py @@ -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: