Use unicode() when serializing REST API errors

Fixes bug: #1226244

Change-Id: Iabbf286cd35522e30fbb03f56f5274d12288a695
This commit is contained in:
Luis A. Garcia 2013-09-16 22:43:22 +00:00
parent 3dd43b3bde
commit 4ff53ac470
3 changed files with 25 additions and 2 deletions

View 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:

View 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")

View 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)