Fix i18n issues

We usually have two different ways to pass and translate API exceptions
for end user.

```
1. with msg attribute
raise
    exec.HTTPBadRequest(explanation=exception.msg)
2. with six.text_type
raise
    exec.HTTPBadRequest(explanation=six.text_type(exception))
```

But currently the second one can't be translated because we force
translate the msg object in our ``__unicode__`` method too early,
please read [1] for detail, fix it.

[1] https://docs.openstack.org/developer/oslo.i18n/guidelines.html#avoid-forcing-the-translation-of-translatable-variables

Change-Id: I043c549f95a44c89f8914520fd86c58a87fd5a7d
Closes-Bug: #1698108
This commit is contained in:
TommyLike 2017-06-15 16:50:25 +08:00
parent d9396742df
commit 538e953eff
2 changed files with 12 additions and 2 deletions

View File

@ -188,7 +188,7 @@ class VolumeActionsController(wsgi.Controller):
connector)
except exception.InvalidInput as err:
raise webob.exc.HTTPBadRequest(
explanation=err)
explanation=err.msg)
except exception.VolumeBackendAPIException:
msg = _("Unable to fetch connection information from backend.")
raise webob.exc.HTTPInternalServerError(explanation=msg)

View File

@ -98,6 +98,9 @@ class CinderException(Exception):
for k, v in self.kwargs.items():
if isinstance(v, Exception):
# NOTE(tommylikehu): If this is a cinder exception it will
# return the msg object, so we won't be preventing
# translations.
self.kwargs[k] = six.text_type(v)
if self._should_format():
@ -117,6 +120,9 @@ class CinderException(Exception):
# at least get the core message out if something happened
message = self.message
elif isinstance(message, Exception):
# NOTE(tommylikehu): If this is a cinder exception it will
# return the msg object, so we won't be preventing
# translations.
message = six.text_type(message)
# NOTE(luisg): We put the actual message in 'msg' so that we can access
@ -128,8 +134,12 @@ class CinderException(Exception):
def _should_format(self):
return self.kwargs['message'] is None or '%(message)' in self.message
# NOTE(tommylikehu): self.msg is already an unicode compatible object
# as the __init__ method ensures of it, and we should not be modifying
# it in any way with str(), unicode(), or six.text_type() as we would
# be preventing translations from happening.
def __unicode__(self):
return six.text_type(self.msg)
return self.msg
class VolumeBackendAPIException(CinderException):