]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Use unicode() when serializing REST API errors
authorLuis A. Garcia <luis@linux.vnet.ibm.com>
Mon, 16 Sep 2013 22:43:22 +0000 (22:43 +0000)
committerLuis A. Garcia <luis@linux.vnet.ibm.com>
Thu, 19 Sep 2013 17:49:50 +0000 (17:49 +0000)
Fixes bug: #1226244

Change-Id: Iabbf286cd35522e30fbb03f56f5274d12288a695

heat/api/middleware/fault.py
heat/common/exception.py
heat/tests/test_fault_middleware.py

index eda705c42c04b5037e745d1e6d0ee2d337d0aab0..466ef92e4a60bd0923652872527191c024d1c0de 100644 (file)
@@ -94,10 +94,10 @@ class FaultWrapper(wsgi.Middleware):
         if ex_type.endswith(rpc_common._REMOTE_POSTFIX):
             ex_type = ex_type[:-len(rpc_common._REMOTE_POSTFIX)]
 
-        message = str(ex.message)
+        message = unicode(ex.message)
 
         if cfg.CONF.debug and not trace:
-            trace = str(ex)
+            trace = unicode(ex)
             if trace.find('\n') > -1:
                 unused, trace = trace.split('\n', 1)
             else:
index 22bc4f8ead92b81cb8d8261205daefb01400531e..170ba70823aacd0de3155a597ffa315e10a6c827 100644 (file)
@@ -120,6 +120,9 @@ class HeatException(Exception):
     def __str__(self):
         return str(self.message)
 
+    def __unicode__(self):
+        return unicode(self.message)
+
 
 class MissingCredentialError(HeatException):
     message = _("Missing required credential: %(required)s")
index 9fe55191ec513ddb1adc1c1aaa78d2b42cdc5eb0..b2c079c427264361b8f8957de8b59f0ac3f12669 100644 (file)
@@ -47,6 +47,26 @@ class FaultMiddlewareTest(HeatTestCase):
                     'title': 'Internal Server Error'}
         self.assertEqual(msg, expected)
 
+    def test_exception_with_non_ascii_chars(self):
+        # We set debug to true to test the code path for serializing traces too
+        cfg.CONF.set_override('debug', True)
+        msg = u'Error with non-ascii chars \x80'
+
+        class TestException(heat_exc.HeatException):
+            message = msg
+
+        wrapper = fault.FaultWrapper(None)
+        msg = wrapper._error(TestException())
+        expected = {'code': 500,
+                    'error': {'message': u'Error with non-ascii chars \x80',
+                              'traceback': 'None\n',
+                              'type': 'TestException'},
+                    'explanation': ('The server has either erred or is '
+                                    'incapable of performing the requested '
+                                    'operation.'),
+                    'title': 'Internal Server Error'}
+        self.assertEqual(msg, expected)
+
     def test_remote_exception(self):
         # We want tracebacks
         cfg.CONF.set_override('debug', True)