Improve error messages in the management tools when external command-line tools are run

This commit is contained in:
Joshua Tauberer 2022-12-13 08:03:39 -05:00
parent 20ec6c2080
commit 26709a3c1d
1 changed files with 9 additions and 6 deletions

View File

@ -122,13 +122,16 @@ def shell(method, cmd_args, env={}, capture_stderr=False, return_bytes=False, tr
if method == "check_output" and input is not None: if method == "check_output" and input is not None:
kwargs['input'] = input kwargs['input'] = input
if not trap: try:
ret = getattr(subprocess, method)(cmd_args, **kwargs) ret = getattr(subprocess, method)(cmd_args, **kwargs)
else: code = 0
try: except subprocess.CalledProcessError as e:
ret = getattr(subprocess, method)(cmd_args, **kwargs) if not trap:
code = 0 # Reformat exception.
except subprocess.CalledProcessError as e: msg = "Command failed with exit code {}: {}".format(e.returncode, subprocess.list2cmdline(cmd_args))
if e.output: msg += "\n\nOutput:\n" + e.output
raise Exception(msg)
else:
ret = e.output ret = e.output
code = e.returncode code = e.returncode
if not return_bytes and isinstance(ret, bytes): ret = ret.decode("utf8") if not return_bytes and isinstance(ret, bytes): ret = ret.decode("utf8")