Merge "Fixing UnicodeEncodeError against volume creating function"

This commit is contained in:
Jenkins 2013-09-13 06:14:49 +00:00 committed by Gerrit Code Review
commit cd3c5cf9b0
2 changed files with 36 additions and 1 deletions

View File

@ -80,6 +80,27 @@ class CreateVolumeFlowTestCase(test.TestCase):
self.counter = float(0)
self.stubs.Set(time, 'time', self.time_inc)
def test_exception_to_unicode(self):
class FakeException(Exception):
def __str__(self):
raise UnicodeError()
exc = Exception('error message')
ret = create_volume._exception_to_unicode(exc)
self.assertEqual(unicode, type(ret))
self.assertEqual(ret, 'error message')
exc = Exception('\xa5 error message')
ret = create_volume._exception_to_unicode(exc)
self.assertEqual(unicode, type(ret))
self.assertEqual(ret, ' error message')
unicodeExc = FakeException('\xa5 error message')
ret = create_volume._exception_to_unicode(unicodeExc)
self.assertEqual(unicode, type(ret))
self.assertEqual(ret, _("Caught '%(exception)s' exception.") %
{'exception': 'FakeException'})
def test_cast_create_volume(self):
props = {}

View File

@ -30,6 +30,7 @@ from cinder.openstack.common import excutils
from cinder.openstack.common import log as logging
from cinder.openstack.common.notifier import api as notifier
from cinder.openstack.common import processutils
from cinder.openstack.common import strutils
from cinder.openstack.common import timeutils
from cinder import policy
from cinder import quota
@ -147,6 +148,18 @@ def _error_out_volume(context, db, volume_id, reason=None):
'update': update})
def _exception_to_unicode(exc):
try:
return unicode(exc)
except UnicodeError:
try:
return strutils.safe_decode(str(exc), errors='ignore')
except UnicodeError:
msg = (_("Caught '%(exception)s' exception.") %
{"exception": exc.__class__.__name__})
return strutils.safe_decode(msg, errors='ignore')
class ExtractVolumeRequestTask(base.CinderTask):
"""Processes an api request values into a validated set of values.
@ -868,7 +881,8 @@ class OnFailureRescheduleTask(base.CinderTask):
"attempt %(num)d due to %(reason)s") %
{'volume_id': volume_id,
'method': _make_pretty_name(create_volume),
'num': num_attempts, 'reason': unicode(cause.exc)})
'num': num_attempts,
'reason': _exception_to_unicode(cause.exc)})
if all(cause.exc_info):
# Stringify to avoid circular ref problem in json serialization