Make Exception.format_message aware of Messages

Many API extensions are casting NovaExceptions
directly to unicode with format_message(),
which results in a loss of localization information
from the possibly wrapped Message objects.

This change lets Messages be returned from
NovaException.format_message() if possible, which
will then be translated and resolved to unicode
later when needed.

fixes bug 1229410

Change-Id: I818a35b1d7d910c4f058d870e69c5ff73e45c6bb
This commit is contained in:
Matt Odden 2013-09-23 12:27:04 +00:00 committed by Gerrit Code Review
parent 61e6058bfd
commit 4a33d66d8f
2 changed files with 13 additions and 4 deletions

View File

@ -136,10 +136,9 @@ class NovaException(Exception):
super(NovaException, self).__init__(message)
def format_message(self):
if self.__class__.__name__.endswith('_Remote'):
return self.args[0]
else:
return unicode(self)
# NOTE(mrodden): use the first argument to the python Exception object
# which should be our full NovaException message, (see __init__)
return self.args[0]
class EncryptionFailure(NovaException):

View File

@ -20,6 +20,7 @@ import inspect
from nova import context
from nova import exception
from nova.openstack.common import gettextutils
from nova import test
@ -141,6 +142,15 @@ class NovaExceptionTestCase(test.NoDBTestCase):
exc = FakeNovaException_Remote(lame_arg='lame')
self.assertEquals(exc.format_message(), "some message %(somearg)s")
def test_format_message_gettext_msg_returned(self):
class FakeNovaException(exception.NovaException):
msg_fmt = gettextutils.Message("Some message %(param)s", 'nova')
exc = FakeNovaException(param='blah')
msg = exc.format_message()
self.assertIsInstance(msg, gettextutils.Message)
self.assertEqual(msg, "Some message blah")
class ExceptionTestCase(test.NoDBTestCase):
@staticmethod