Add __repr__ for NovaException

Due to change c1a7079c26d27a2e46cca26963d3d9aa040bdbe8 in
oslo.serialization, serialized exceptions in errors no longer
have kwargs. This change fixes the problem by adding __repr__
to NovaException which will return a string representation
of exception.__dict__. This string contains the exception message
along with all the kwargs. Without __repr__, the serialization
process for exceptions just returns the exception type and the
default message, without the kwargs.

Change-Id: I653282a030d03362dfca0fd1026cebe920d54e37
Closes-Bug: #1756360
This commit is contained in:
Tyler Blakeslee 2018-03-23 08:21:27 -06:00
parent 000391041f
commit cc457dfffa
2 changed files with 22 additions and 0 deletions

View File

@ -102,6 +102,11 @@ class NovaException(Exception):
# which should be our full NovaException message, (see __init__)
return self.args[0]
def __repr__(self):
dict_repr = self.__dict__
dict_repr['class'] = self.__class__.__name__
return str(dict_repr)
class EncryptionFailure(NovaException):
msg_fmt = _("Failed to encrypt text: %(reason)s")

View File

@ -229,6 +229,23 @@ class NovaExceptionTestCase(test.NoDBTestCase):
exc = FakeNovaException_Remote(lame_arg='lame')
self.assertEqual("some message %(somearg)s", exc.format_message())
def test_repr(self):
class FakeNovaException(exception.NovaException):
msg_fmt = "some message"
mock_exc = FakeNovaException(code=500)
exc_repr = repr(mock_exc)
eval_repr = eval(exc_repr)
exc_kwargs = eval_repr.get('kwargs')
self.assertIsNotNone(exc_kwargs)
self.assertEqual(500, exc_kwargs.get('code'))
self.assertEqual('some message', eval_repr.get('message'))
self.assertEqual('FakeNovaException', eval_repr.get('class'))
class ConvertedExceptionTestCase(test.NoDBTestCase):
def test_instantiate(self):