Fix TypeError: __str__ returned non-string (type ImageRefValidationFailed)

This change contains two fixes for the same issue:
* Do not pass an instance of ImageRefValidationFailed as a message
  to an exception constructor.
* Make sure that IronicException.__str__ always returns an str,
  even when a non-string is passed as the first argument to __init__.

Change-Id: I96edb28955e64915e9d6a481634857fd27690555
Story: #2003682
Task: #26206
This commit is contained in:
Dmitry Tantsur 2019-03-04 13:07:54 +01:00
parent 3f6d4c6a78
commit 245af384ff
5 changed files with 22 additions and 9 deletions

View File

@ -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."""

View File

@ -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:

View File

@ -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)

View File

@ -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):

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Returns the correct error message on providing an invalid reference to
``image_source``. Previously an internal error was raised.