When creating a volume with non-English characters and there is
not enough space for it, then an UnicodeEncodeError occurs. So
we need convert any coded exception message to unicode string
for logging.
Fixes bug
1223128
Change-Id: If1d4faad2bcde696f20565deb01226de33caccad
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 = {}
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
'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.
"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