Skip to content

Commit

Permalink
fix: Setup exception handler for CommandFailedError only
Browse files Browse the repository at this point in the history
* Match only certain module lib paths
  • Loading branch information
gavindsouza committed May 26, 2023
1 parent 8175239 commit 94a1b8c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions bench/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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
Expand All @@ -38,6 +40,7 @@

change_uid_msg = "You should not run this command as root"
src = os.path.dirname(__file__)
SKIP_MODULE_TRACEBACK = ("click",)


@contextmanager
Expand Down Expand Up @@ -246,13 +249,22 @@ def _chdir(*args, **kwargs):

def setup_exception_handler():
from traceback import format_exception
from bench.exceptions import CommandFailedError

def handle_exception(exc_type, exc_info, tb):
print("".join(generate_exc(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):
for t in format_exception(exc_type, exc_info, tb):
if "/click/" not in t:
yield t
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

0 comments on commit 94a1b8c

Please sign in to comment.