]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
CinderException args to strings when exceptions
authorgit-harry <git-harry@live.co.uk>
Thu, 17 Apr 2014 09:49:49 +0000 (10:49 +0100)
committergit-harry <git-harry@live.co.uk>
Tue, 29 Apr 2014 11:36:57 +0000 (12:36 +0100)
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
cinder/tests/test_exception.py

index 553f06e71e62a4c82283795294bc58ac96371310..034df564ca7fd96d8c5b37b0e00a4bcedae6af29 100644 (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
index 560e3d83df3a48cc8151079710172449a0ede67e..88d442ec48bf3f68f065e4923a89c50c5581b25d 100644 (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'])