From 2b3c1d5bfc8368d88d8871a554a8cb253ae5b2ae Mon Sep 17 00:00:00 2001 From: Alex Harvey Date: Mon, 18 Oct 2021 15:53:19 +1100 Subject: [PATCH] Stop hiding critical debug info in helpers (#988) (#997) * [Resolve #988] Stop hiding debug info in helpers Before this, the catch_exceptions function in cli/helpers would catch a range of exceptions and then hide all but the error message from the caller. Over the years, this has caused myself and others quite a lot of lost time, as it is now often quite unclear what caused Sceptre to fail. Simply re-raising the original exception provides valuable information to allow users of Sceptre to debug their failing code. * Refactor according to Jon's suggestion * Refactor to implement Craig Hurley's suggestion * Restore master version of tests * Add unit tests --- sceptre/cli/helpers.py | 9 ++++++++- tests/test_cli.py | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/sceptre/cli/helpers.py b/sceptre/cli/helpers.py index b5749c529..fd82a2c16 100644 --- a/sceptre/cli/helpers.py +++ b/sceptre/cli/helpers.py @@ -27,16 +27,23 @@ def catch_exceptions(func): simplified. :returns: The decorated function. """ + def logging_level(): + logger = logging.getLogger(__name__) + return logger.getEffectiveLevel() + @wraps(func) def decorated(*args, **kwargs): """ Invokes ``func``, catches expected errors, prints the error message and - exits sceptre with a non-zero exit code. + exits sceptre with a non-zero exit code. In debug mode, the original + exception is re-raised to assist debugging. """ try: return func(*args, **kwargs) except (SceptreException, BotoCoreError, ClientError, Boto3Error, TemplateError) as error: + if logging_level() == logging.DEBUG: + raise write(error) sys.exit(1) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1fe02ac21..e90aac5fe 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -56,7 +56,7 @@ def teardown_method(self, test_method): self.patcher_StackActions.stop() @patch("sys.exit") - def test_catch_excecptions(self, mock_exit): + def test_catch_exceptions(self, mock_exit): @catch_exceptions def raises_exception(): raise SceptreException() @@ -64,6 +64,17 @@ def raises_exception(): raises_exception() mock_exit.assert_called_once_with(1) + def test_catch_exceptions_debug_mode(self): + @catch_exceptions + def raises_exception(): + raise SceptreException() + + logger = logging.getLogger("sceptre") + logger.setLevel(logging.DEBUG) + + with pytest.raises(SceptreException): + raises_exception() + @pytest.mark.parametrize("command,files,output", [ # one --var option (