2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-22 22:58:31 +00:00

Merge pull request #1455 from gavindsouza/cleaner-cli

fix(cli): Show relevant details on CommandFailedError
This commit is contained in:
Ritwik Puri 2023-05-30 12:13:54 +05:30 committed by GitHub
commit 3e448d4d42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -28,6 +28,8 @@ from bench.utils import (
get_cmd_from_sysargv,
)
from bench.utils.bench import get_env_cmd
from importlib.util import find_spec
# these variables are used to show dynamic outputs on the terminal
dynamic_feed = False
@ -38,6 +40,7 @@ bench.LOG_BUFFER = []
change_uid_msg = "You should not run this command as root"
src = os.path.dirname(__file__)
SKIP_MODULE_TRACEBACK = ("click",)
@contextmanager
@ -118,6 +121,8 @@ def cli():
_opts = [x.opts + x.secondary_opts for x in bench_command.params]
opts = {item for sublist in _opts for item in sublist}
setup_exception_handler()
# handle usages like `--use-feature='feat-x'` and `--use-feature 'feat-x'`
if cmd_from_sys and cmd_from_sys.split("=", 1)[0].strip() in opts:
bench_command()
@ -240,3 +245,26 @@ def setup_clear_cache():
return f(*args, **kwargs)
os.chdir = _chdir
def setup_exception_handler():
from traceback import format_exception
from bench.exceptions import CommandFailedError
def handle_exception(exc_type, exc_info, tb):
if exc_type == CommandFailedError:
print("".join(generate_exc(exc_type, exc_info, tb)))
else:
sys.__excepthook__(exc_type, exc_info, tb)
def generate_exc(exc_type, exc_info, tb):
TB_SKIP = [
os.path.dirname(find_spec(module).origin) for module in SKIP_MODULE_TRACEBACK
]
for tb_line in format_exception(exc_type, exc_info, tb):
for skip_module in TB_SKIP:
if skip_module not in tb_line:
yield tb_line
sys.excepthook = handle_exception

View File

@ -155,7 +155,7 @@ def exec_cmd(cmd, cwd=".", env=None, _raise=True):
if return_code:
logger.warning(f"{cmd_log} executed with exit code {return_code}")
if _raise:
raise CommandFailedError from subprocess.CalledProcessError(return_code, cmd)
raise CommandFailedError(cmd) from subprocess.CalledProcessError(return_code, cmd)
return return_code