Merge "Enhance exception translation to better handle NeutronExceptions"
This commit is contained in:
commit
1bf5e729c9
@ -147,9 +147,12 @@ def translate(translatable, locale):
|
|||||||
was not translated
|
was not translated
|
||||||
"""
|
"""
|
||||||
localize = gettextutils.get_localized_message
|
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)
|
translatable.message = localize(translatable.message, locale)
|
||||||
if isinstance(translatable, webob.exc.HTTPError):
|
else:
|
||||||
translatable.detail = localize(translatable.detail, locale)
|
return localize(translatable, locale)
|
||||||
return translatable
|
return translatable
|
||||||
return localize(translatable, locale)
|
|
||||||
|
@ -34,6 +34,7 @@ class NeutronException(Exception):
|
|||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
try:
|
try:
|
||||||
super(NeutronException, self).__init__(self.message % kwargs)
|
super(NeutronException, self).__init__(self.message % kwargs)
|
||||||
|
self.msg = self.message % kwargs
|
||||||
except Exception:
|
except Exception:
|
||||||
if _FATAL_EXCEPTION_FORMAT_ERRORS:
|
if _FATAL_EXCEPTION_FORMAT_ERRORS:
|
||||||
raise
|
raise
|
||||||
@ -41,6 +42,9 @@ class NeutronException(Exception):
|
|||||||
# at least get the core message out if something happened
|
# at least get the core message out if something happened
|
||||||
super(NeutronException, self).__init__(self.message)
|
super(NeutronException, self).__init__(self.message)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return unicode(self.msg)
|
||||||
|
|
||||||
|
|
||||||
class BadRequest(NeutronException):
|
class BadRequest(NeutronException):
|
||||||
message = _('Bad %(resource)s request: %(msg)s')
|
message = _('Bad %(resource)s request: %(msg)s')
|
||||||
|
@ -160,8 +160,7 @@ class ResourceTestCase(base.BaseTestCase):
|
|||||||
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
|
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
|
||||||
expected_res)
|
expected_res)
|
||||||
|
|
||||||
@mock.patch('neutron.openstack.common.gettextutils.Message.data',
|
@mock.patch('neutron.openstack.common.gettextutils.get_localized_message')
|
||||||
new_callable=mock.PropertyMock)
|
|
||||||
def test_unmapped_neutron_error_localized(self, mock_translation):
|
def test_unmapped_neutron_error_localized(self, mock_translation):
|
||||||
gettextutils.install('blaa', lazy=True)
|
gettextutils.install('blaa', lazy=True)
|
||||||
msg_translation = 'Translated error'
|
msg_translation = 'Translated error'
|
||||||
@ -223,8 +222,7 @@ class ResourceTestCase(base.BaseTestCase):
|
|||||||
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
|
self.assertEqual(wsgi.XMLDeserializer().deserialize(res.body),
|
||||||
expected_res)
|
expected_res)
|
||||||
|
|
||||||
@mock.patch('neutron.openstack.common.gettextutils.Message.data',
|
@mock.patch('neutron.openstack.common.gettextutils.get_localized_message')
|
||||||
new_callable=mock.PropertyMock)
|
|
||||||
def test_mapped_neutron_error_localized(self, mock_translation):
|
def test_mapped_neutron_error_localized(self, mock_translation):
|
||||||
gettextutils.install('blaa', lazy=True)
|
gettextutils.install('blaa', lazy=True)
|
||||||
msg_translation = 'Translated error'
|
msg_translation = 'Translated error'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user