Do not use str() on exceptions (#46950)

This commit is contained in:
Martin Krizek 2018-11-09 07:59:30 +01:00 committed by GitHub
commit a80c25cbd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 76 additions and 66 deletions

View file

@ -268,7 +268,7 @@ from distutils.version import LooseVersion
from ansible.module_utils.basic import AnsibleModule, get_module_path
from ansible.module_utils.six import b, string_types
from ansible.module_utils._text import to_native
from ansible.module_utils._text import to_native, to_text
def relocate_repo(module, result, repo_dir, old_repo_dir, worktree_dir):
@ -286,7 +286,7 @@ def relocate_repo(module, result, repo_dir, old_repo_dir, worktree_dir):
# if we already moved the .git dir, roll it back
if os.path.exists(repo_dir):
shutil.move(repo_dir, old_repo_dir)
module.fail_json(msg='Unable to move git dir. %s' % str(err))
module.fail_json(msg=u'Unable to move git dir. %s' % to_text(err))
def head_splitter(headfile, remote, module=None, fail_on_error=False):
@ -690,7 +690,7 @@ def get_head_branch(git_path, module, dest, remote, bare=False):
module.fail_json(
msg='Current repo does not have a valid reference to a '
'separate Git dir or it refers to the invalid path',
details=str(err),
details=to_text(err),
)
# Read .git/HEAD for the name of the branch.
# If we're in a detached HEAD state, look up the branch associated with
@ -992,8 +992,8 @@ def create_archive(git_path, module, dest, archive, version, repo, result):
except OSError as e:
module.fail_json(msg="Failed to move %s to %s" %
(new_archive, archive),
details="Error occured while moving : %s"
% to_native(e))
details=u"Error occured while moving : %s"
% to_text(e))
else:
# Perform archive from local directory
git_archive(git_path, module, dest, archive, archive_fmt, version)
@ -1100,7 +1100,7 @@ def main():
module.fail_json(
msg='Current repo does not have a valid reference to a '
'separate Git dir or it refers to the invalid path',
details=str(err),
details=to_text(err),
)
gitconfig = os.path.join(repo_path, 'config')

View file

@ -65,7 +65,7 @@ except ImportError:
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import binary_type
from ansible.module_utils._text import to_bytes
from ansible.module_utils._text import to_bytes, to_text
def has_boolean_value(module, name):
@ -238,7 +238,7 @@ def semanage_boolean_value(module, name, state):
semanage_commit(module, handle)
semanage_destroy_handle(module, handle)
except Exception as e:
module.fail_json(msg="Failed to manage policy for boolean %s: %s" % (name, str(e)))
module.fail_json(msg=u"Failed to manage policy for boolean %s: %s" % (name, to_text(e)))
return changed

View file

@ -282,7 +282,7 @@ class SysctlModule(object):
rc, out, err = self.module.run_command(sysctl_args)
if rc != 0:
self.module.fail_json(msg="Failed to reload sysctl: %s" % str(out) + str(err))
self.module.fail_json(msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err))
# ==============================================================
# SYSCTL FILE MANAGEMENT
@ -297,7 +297,7 @@ class SysctlModule(object):
with open(self.sysctl_file, "r") as read_file:
lines = read_file.readlines()
except IOError as e:
self.module.fail_json(msg="Failed to open %s: %s" % (self.sysctl_file, to_native(e)))
self.module.fail_json(msg="Failed to open %s: %s" % (to_native(self.sysctl_file), to_native(e)))
for line in lines:
line = line.strip()

View file

@ -19,6 +19,8 @@ import signal
import time
import syslog
from ansible.module_utils._text import to_text
PY3 = sys.version_info[0] == 3
syslog.openlog('ansible-%s' % os.path.basename(__file__))
@ -164,7 +166,7 @@ def _run_module(wrapped_cmd, jid, job_path):
result = {
"failed": 1,
"cmd": wrapped_cmd,
"msg": str(e),
"msg": to_text(e),
"outdata": outdata, # temporary notice only
"stderr": stderr
}