From 26709a3c1dba9cadbd88636b002d95c2f6bd2b14 Mon Sep 17 00:00:00 2001 From: Joshua Tauberer Date: Tue, 13 Dec 2022 08:03:39 -0500 Subject: [PATCH] Improve error messages in the management tools when external command-line tools are run --- management/utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/management/utils.py b/management/utils.py index b5ca7e59..f452c16d 100644 --- a/management/utils.py +++ b/management/utils.py @@ -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: kwargs['input'] = input - if not trap: + try: ret = getattr(subprocess, method)(cmd_args, **kwargs) - else: - try: - ret = getattr(subprocess, method)(cmd_args, **kwargs) - code = 0 - except subprocess.CalledProcessError as e: + code = 0 + except subprocess.CalledProcessError as e: + if not trap: + # Reformat exception. + 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 code = e.returncode if not return_bytes and isinstance(ret, bytes): ret = ret.decode("utf8")