Fix 'UnicodeEncodeError' for unicode values

Used '%' instead of format() because "%" formatting operation
returns a unicode string for both unicode and non-unicode
format strings.

Encoded the exception error message in novaclient.shell.main and
novaclient.utils.do_action_on_many method to avoid
"UnicodeEncodeError" and print valid message.

NOTE:
Not used u'{0}.format(<str>) because in python 3 everything
is unicode so no need to add explicit 'u' to format string.

Closes-Bug: #1403379
Change-Id: I13f1ef2e1b41299b417cad3759a5cda241890df7
This commit is contained in:
bhagyashris 2016-07-05 19:24:21 +05:30
parent ef5dc3211d
commit d8c07006dd
3 changed files with 8 additions and 4 deletions

View File

@ -982,9 +982,13 @@ def main():
OpenStackComputeShell().main(argv)
except Exception as exc:
logger.debug(exc, exc_info=1)
print(_("ERROR (%(type)s): %(msg)s") % {
if six.PY2:
message = encodeutils.safe_encode(six.text_type(exc))
else:
message = encodeutils.exception_to_unicode(exc)
print("ERROR (%(type)s): %(msg)s" % {
'type': exc.__class__.__name__,
'msg': encodeutils.exception_to_unicode(exc)},
'msg': message},
file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:

View File

@ -407,7 +407,7 @@ def do_action_on_many(action, resources, success_msg, error_msg):
print(success_msg % resource)
except Exception as e:
failure_flag = True
print(e)
print(encodeutils.safe_encode(six.text_type(e)))
if failure_flag:
raise exceptions.CommandError(error_msg)

View File

@ -76,7 +76,7 @@ class HostManager(base.ManagerWithFind):
:param action: The action to perform
returns: An instance of novaclient.base.TupleWithMeta
"""
url = '/os-hosts/{0}/{1}'.format(host, action)
url = '/os-hosts/%s/%s' % (host, action)
resp, body = self.api.client.get(url)
return base.TupleWithMeta((resp, body), resp)