Skip to content

Commit

Permalink
Enhance exception translation to better handle NeutronExceptions
Browse files Browse the repository at this point in the history
NeutronExceptions have a 'message' class attribute that holds the
generic error message template, e.g. "Network %(network)s not found",
unfortunately, because the names are the same, it was overshadowing the
actual exception instance 'message', e.g. "Network 1 not found", after
translation when the exception was serialized to JSON.

This patch puts the exception's actual message in a new field called
'msg' and overwrites NeutronException unicode() so that 'msg' is used
during serialization and we'll get the correct message on the REST API
response.

Fixes bug: #1212882

Change-Id: I3965bffb1c2c2eee0af440d1ecd30ccb3bb958d5
  • Loading branch information
Luis A. Garcia authored and openstack-gerrit committed Sep 12, 2013
1 parent ca99b92 commit 7e2622c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
13 changes: 8 additions & 5 deletions neutron/api/v2/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ def translate(translatable, locale):
was not translated
"""
localize = gettextutils.get_localized_message
if isinstance(translatable, Exception):
if isinstance(translatable, exceptions.NeutronException):
translatable.msg = localize(translatable.msg, locale)
elif isinstance(translatable, webob.exc.HTTPError):
translatable.detail = localize(translatable.detail, locale)
elif isinstance(translatable, Exception):
translatable.message = localize(translatable.message, locale)
if isinstance(translatable, webob.exc.HTTPError):
translatable.detail = localize(translatable.detail, locale)
return translatable
return localize(translatable, locale)
else:
return localize(translatable, locale)
return translatable
4 changes: 4 additions & 0 deletions neutron/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ class NeutronException(Exception):
def __init__(self, **kwargs):
try:
super(NeutronException, self).__init__(self.message % kwargs)
self.msg = self.message % kwargs
except Exception:
if _FATAL_EXCEPTION_FORMAT_ERRORS:
raise
else:
# at least get the core message out if something happened
super(NeutronException, self).__init__(self.message)

def __unicode__(self):
return unicode(self.msg)


class BadRequest(NeutronException):
message = _('Bad %(resource)s request: %(msg)s')
Expand Down
6 changes: 2 additions & 4 deletions neutron/tests/unit/test_api_v2_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ class TestException(q_exc.NeutronException):
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
expected_res)

@mock.patch('neutron.openstack.common.gettextutils.Message.data',
new_callable=mock.PropertyMock)
@mock.patch('neutron.openstack.common.gettextutils.get_localized_message')
def test_unmapped_neutron_error_localized(self, mock_translation):
gettextutils.install('blaa', lazy=True)
msg_translation = 'Translated error'
Expand Down Expand Up @@ -223,8 +222,7 @@ class TestException(q_exc.NeutronException):
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
expected_res)

@mock.patch('neutron.openstack.common.gettextutils.Message.data',
new_callable=mock.PropertyMock)
@mock.patch('neutron.openstack.common.gettextutils.get_localized_message')
def test_mapped_neutron_error_localized(self, mock_translation):
gettextutils.install('blaa', lazy=True)
msg_translation = 'Translated error'
Expand Down

0 comments on commit 7e2622c

Please sign in to comment.