diff --git a/ironic/common/exception.py b/ironic/common/exception.py index e5630990c5..4cdbb03dd4 100644 --- a/ironic/common/exception.py +++ b/ironic/common/exception.py @@ -128,10 +128,12 @@ class IronicException(Exception): def __str__(self): """Encode to utf-8 then wsme api can consume it as well.""" - if not six.PY3: - return six.text_type(self.args[0]).encode('utf-8') - - return self.args[0] + value = self.__unicode__() + if six.PY3: + # On Python 3 unicode is the same as str + return value + else: + return value.encode('utf-8') def __unicode__(self): """Return a unicode representation of the exception message.""" diff --git a/ironic/drivers/modules/deploy_utils.py b/ironic/drivers/modules/deploy_utils.py index f8b81e9f77..30c8ae6528 100644 --- a/ironic/drivers/modules/deploy_utils.py +++ b/ironic/drivers/modules/deploy_utils.py @@ -829,7 +829,7 @@ def validate_image_properties(ctx, deploy_info, properties): raise exception.InvalidParameterValue(_( "Image %s can not be found.") % image_href) except exception.ImageRefValidationFailed as e: - raise exception.InvalidParameterValue(e) + raise exception.InvalidParameterValue(err=e) missing_props = [] for prop in properties: diff --git a/ironic/tests/unit/common/test_exception.py b/ironic/tests/unit/common/test_exception.py index 17a80b9fed..5e7f6bcd60 100644 --- a/ironic/tests/unit/common/test_exception.py +++ b/ironic/tests/unit/common/test_exception.py @@ -32,7 +32,7 @@ class TestException(exception.IronicException): class TestIronicException(base.TestCase): - def test___init__(self): + def test___str__encoding(self): expected = b'\xc3\xa9\xe0\xaf\xb2\xe0\xbe\x84' if six.PY3: expected = expected.decode('utf-8') @@ -40,6 +40,11 @@ class TestIronicException(base.TestCase): exc = exception.IronicException(message) self.assertEqual(expected, exc.__str__()) + def test___str__non_string(self): + exc = exception.IronicException(42) + self.assertEqual("42", exc.__str__()) + self.assertEqual(u"42", exc.__unicode__()) + @mock.patch.object(exception.LOG, 'error', autospec=True) def test___init___invalid_kwarg(self, log_mock): self.config(fatal_exception_format_errors=False) diff --git a/ironic/tests/unit/drivers/modules/test_deploy_utils.py b/ironic/tests/unit/drivers/modules/test_deploy_utils.py index 4f6d828103..779605de6a 100644 --- a/ironic/tests/unit/drivers/modules/test_deploy_utils.py +++ b/ironic/tests/unit/drivers/modules/test_deploy_utils.py @@ -1955,9 +1955,10 @@ class ValidateImagePropertiesTestCase(db_base.DbTestCase): driver_internal_info=DRV_INTERNAL_INFO_DICT, ) inst_info = utils.get_image_instance_info(node) - self.assertRaises(exception.InvalidParameterValue, - utils.validate_image_properties, self.context, - inst_info, ['kernel', 'ramdisk']) + self.assertRaisesRegex(exception.InvalidParameterValue, + 'HTTPError', + utils.validate_image_properties, self.context, + inst_info, ['kernel', 'ramdisk']) class ValidateParametersTestCase(db_base.DbTestCase): diff --git a/releasenotes/notes/type-error-str-6826c53d7e5e1243.yaml b/releasenotes/notes/type-error-str-6826c53d7e5e1243.yaml new file mode 100644 index 0000000000..e144f1c1a1 --- /dev/null +++ b/releasenotes/notes/type-error-str-6826c53d7e5e1243.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Returns the correct error message on providing an invalid reference to + ``image_source``. Previously an internal error was raised.