Skip to content

Commit

Permalink
Stop hiding critical debug info in helpers (#988) (#997)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
alexharv074 committed Oct 18, 2021
1 parent 8a3eb94 commit 2b3c1d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion sceptre/cli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
13 changes: 12 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,25 @@ 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()

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
(
Expand Down

0 comments on commit 2b3c1d5

Please sign in to comment.