From 31a09451394458273ae1ed9af867476da4ece9d6 Mon Sep 17 00:00:00 2001 From: git-harry Date: Thu, 17 Apr 2014 10:49:49 +0100 Subject: [PATCH] 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 --- cinder/exception.py | 7 +++++++ cinder/tests/test_exception.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cinder/exception.py b/cinder/exception.py index 553f06e71..034df564c 100644 --- a/cinder/exception.py +++ b/cinder/exception.py @@ -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 diff --git a/cinder/tests/test_exception.py b/cinder/tests/test_exception.py index 560e3d83d..88d442ec4 100644 --- a/cinder/tests/test_exception.py +++ b/cinder/tests/test_exception.py @@ -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']) -- 2.45.2