CinderException args to strings when exceptions

CinderException message and kwargs are converted to strings if the
argument is itself an exception.

Exceptions passed as arguments to CinderException happen in many areas
of the code. This causes an error when oslo.messaging tries to serialise
the exception.

Change-Id: I1399c939bbca47ab8362ac3bbe0e9a349c6d5572
Closes-Bug: #1301249
This commit is contained in:
git-harry 2014-04-17 10:49:49 +01:00
parent a221a30b32
commit 31a0945139
2 changed files with 19 additions and 0 deletions

View File

@ -22,6 +22,7 @@ SHOULD include dedicated exception logging.
"""
import six
import sys
from oslo.config import cfg
@ -77,6 +78,10 @@ class CinderException(Exception):
except AttributeError:
pass
for k, v in self.kwargs.iteritems():
if isinstance(v, Exception):
self.kwargs[k] = six.text_type(v)
if not message:
try:
message = self.message % kwargs
@ -92,6 +97,8 @@ class CinderException(Exception):
raise exc_info[0], exc_info[1], exc_info[2]
# at least get the core message out if something happened
message = self.message
elif isinstance(message, Exception):
message = six.text_type(message)
# NOTE(luisg): We put the actual message in 'msg' so that we can access
# it, because if we try to access the message via 'message' it will be

View File

@ -89,3 +89,15 @@ class CinderExceptionTestCase(test.TestCase):
exc = FakeCinderException(code=404)
self.assertEqual(exc.kwargs['code'], 404)
def test_error_msg_is_exception_to_string(self):
msg = 'test message'
exc1 = Exception(msg)
exc2 = exception.CinderException(exc1)
self.assertEqual(msg, exc2.msg)
def test_exception_kwargs_to_string(self):
msg = 'test message'
exc1 = Exception(msg)
exc2 = exception.CinderException(kwarg1=exc1)
self.assertEqual(msg, exc2.kwargs['kwarg1'])