From d1da173017216a4506d93203a3c1c2fb6bba96b8 Mon Sep 17 00:00:00 2001 From: Liang Chen Date: Sun, 14 Apr 2013 22:44:12 +0800 Subject: Enable unicode error message Keystone exceptions could only take byte string message as the message arguments to construct exception instances because of the way its super class StandardError implements __unicode__. This patch can also make sure it would not unintentionally remove line breaks and indentation in a explicitly given message argument. Fixs bug #1168879 Change-Id: I7916efc87845cfc4dba705e9474125b275affc13 --- keystone/exception.py | 12 ++++-------- tests/test_exception.py | 9 +++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/keystone/exception.py b/keystone/exception.py index eaacfcf0..21844300 100644 --- a/keystone/exception.py +++ b/keystone/exception.py @@ -56,14 +56,10 @@ class Error(StandardError): :raises: KeyError given insufficient kwargs """ - return message or self.__doc__ % kwargs - - def __str__(self): - """Cleans up line breaks and indentation from doc strings.""" - string = super(Error, self).__str__() - string = re.sub('[ \n]+', ' ', string) - string = string.strip() - return string + if not message: + message = re.sub('[ \n]+', ' ', self.__doc__ % kwargs) + message = message.strip() + return message class ValidationError(Error): diff --git a/tests/test_exception.py b/tests/test_exception.py index dffa14ed..4be470eb 100644 --- a/tests/test_exception.py +++ b/tests/test_exception.py @@ -136,3 +136,12 @@ class SecurityErrorTestCase(ExceptionTestCase): e = exception.ForbiddenAction(action=risky_info) self.assertValidJsonRendering(e) self.assertIn(risky_info, str(e)) + + def test_unicode_message(self): + message = u'Comment \xe7a va' + e = exception.Error(message) + self.assertEqual(e.message, message) + try: + unicode(e) + except UnicodeEncodeError: + self.fail("unicode error message not supported") -- cgit