mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #1030 from pre-commit/improved_interrupt_output
Improve output when interrupted (^C)
This commit is contained in:
commit
a889f0bfd5
2 changed files with 31 additions and 6 deletions
|
|
@ -44,9 +44,11 @@ def _log_and_exit(msg, exc, formatted):
|
||||||
def error_handler():
|
def error_handler():
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
except FatalError as e:
|
except (Exception, KeyboardInterrupt) as e:
|
||||||
_log_and_exit('An error has occurred', e, traceback.format_exc())
|
if isinstance(e, FatalError):
|
||||||
except Exception as e:
|
msg = 'An error has occurred'
|
||||||
_log_and_exit(
|
elif isinstance(e, KeyboardInterrupt):
|
||||||
'An unexpected error has occurred', e, traceback.format_exc(),
|
msg = 'Interrupted (^C)'
|
||||||
)
|
else:
|
||||||
|
msg = 'An unexpected error has occurred'
|
||||||
|
_log_and_exit(msg, e, traceback.format_exc())
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,29 @@ def test_error_handler_uncaught_error(mocked_log_and_exit):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_keyboardinterrupt(mocked_log_and_exit):
|
||||||
|
exc = KeyboardInterrupt()
|
||||||
|
with error_handler.error_handler():
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
mocked_log_and_exit.assert_called_once_with(
|
||||||
|
'Interrupted (^C)',
|
||||||
|
exc,
|
||||||
|
# Tested below
|
||||||
|
mock.ANY,
|
||||||
|
)
|
||||||
|
assert re.match(
|
||||||
|
r'Traceback \(most recent call last\):\n'
|
||||||
|
r' File ".+pre_commit.error_handler.py", line \d+, in error_handler\n'
|
||||||
|
r' yield\n'
|
||||||
|
r' File ".+tests.error_handler_test.py", line \d+, '
|
||||||
|
r'in test_error_handler_keyboardinterrupt\n'
|
||||||
|
r' raise exc\n'
|
||||||
|
r'KeyboardInterrupt\n',
|
||||||
|
mocked_log_and_exit.call_args[0][2],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_log_and_exit(cap_out, mock_store_dir):
|
def test_log_and_exit(cap_out, mock_store_dir):
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
error_handler._log_and_exit(
|
error_handler._log_and_exit(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue