mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 01:51:46 +04:00
Add error_handler and use it.
This commit is contained in:
parent
e3d29a897b
commit
9a017dcbe9
8 changed files with 177 additions and 35 deletions
42
pre_commit/error_handler.py
Normal file
42
pre_commit/error_handler.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import print_function
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
import io
|
||||||
|
import os.path
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
from pre_commit.errors import FatalError
|
||||||
|
from pre_commit.store import Store
|
||||||
|
|
||||||
|
|
||||||
|
# For testing purposes
|
||||||
|
class PreCommitSystemExit(SystemExit):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _log_and_exit(msg, exc, formatted, print_fn=print):
|
||||||
|
error_msg = '{0}: {1}: {2}'.format(msg, type(exc).__name__, exc)
|
||||||
|
print_fn(error_msg)
|
||||||
|
print_fn('Check the log at ~/.pre-commit/pre-commit.log')
|
||||||
|
store = Store()
|
||||||
|
store.require_created()
|
||||||
|
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'w') as log:
|
||||||
|
log.write(error_msg + '\n')
|
||||||
|
log.write(formatted + '\n')
|
||||||
|
raise PreCommitSystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def error_handler():
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except FatalError as e:
|
||||||
|
_log_and_exit('An error has occurred', e, traceback.format_exc())
|
||||||
|
except Exception as e:
|
||||||
|
_log_and_exit(
|
||||||
|
'An unexpected error has occurred',
|
||||||
|
e,
|
||||||
|
traceback.format_exc(),
|
||||||
|
)
|
||||||
6
pre_commit/errors.py
Normal file
6
pre_commit/errors.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
|
||||||
|
class FatalError(RuntimeError):
|
||||||
|
pass
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
"""five: six, redux"""
|
|
||||||
# pylint:disable=invalid-name
|
# pylint:disable=invalid-name
|
||||||
PY2 = str is bytes
|
PY2 = str is bytes
|
||||||
PY3 = str is not bytes
|
PY3 = str is not bytes
|
||||||
|
|
|
||||||
|
|
@ -20,20 +20,20 @@ def extend_validator_cls(validator_cls, modify):
|
||||||
|
|
||||||
|
|
||||||
def default_values(properties, instance):
|
def default_values(properties, instance):
|
||||||
for property, subschema in properties.items():
|
for prop, subschema in properties.items():
|
||||||
if 'default' in subschema:
|
if 'default' in subschema:
|
||||||
instance.setdefault(
|
instance.setdefault(
|
||||||
property, copy.deepcopy(subschema['default']),
|
prop, copy.deepcopy(subschema['default']),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def remove_default_values(properties, instance):
|
def remove_default_values(properties, instance):
|
||||||
for property, subschema in properties.items():
|
for prop, subschema in properties.items():
|
||||||
if (
|
if (
|
||||||
'default' in subschema and
|
'default' in subschema and
|
||||||
instance.get(property) == subschema['default']
|
instance.get(prop) == subschema['default']
|
||||||
):
|
):
|
||||||
del instance[property]
|
del instance[prop]
|
||||||
|
|
||||||
|
|
||||||
_AddDefaultsValidator = extend_validator_cls(
|
_AddDefaultsValidator = extend_validator_cls(
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ from pre_commit.commands.clean import clean
|
||||||
from pre_commit.commands.install_uninstall import install
|
from pre_commit.commands.install_uninstall import install
|
||||||
from pre_commit.commands.install_uninstall import uninstall
|
from pre_commit.commands.install_uninstall import uninstall
|
||||||
from pre_commit.commands.run import run
|
from pre_commit.commands.run import run
|
||||||
|
from pre_commit.error_handler import error_handler
|
||||||
from pre_commit.runner import Runner
|
from pre_commit.runner import Runner
|
||||||
from pre_commit.util import entry
|
from pre_commit.util import entry
|
||||||
|
|
||||||
|
|
@ -83,6 +84,7 @@ def main(argv):
|
||||||
else:
|
else:
|
||||||
parser.parse_args(['--help'])
|
parser.parse_args(['--help'])
|
||||||
|
|
||||||
|
with error_handler():
|
||||||
runner = Runner.create()
|
runner = Runner.create()
|
||||||
|
|
||||||
if args.command == 'install':
|
if args.command == 'install':
|
||||||
|
|
|
||||||
2
pylintrc
2
pylintrc
|
|
@ -1,5 +1,5 @@
|
||||||
[MESSAGES CONTROL]
|
[MESSAGES CONTROL]
|
||||||
disable=missing-docstring,abstract-method,redefined-builtin,useless-else-on-loop,redefined-outer-name,invalid-name
|
disable=locally-disabled,fixme,missing-docstring,abstract-method,useless-else-on-loop,invalid-name
|
||||||
|
|
||||||
[REPORTS]
|
[REPORTS]
|
||||||
output-format=colorized
|
output-format=colorized
|
||||||
|
|
|
||||||
93
tests/error_handler_test.py
Normal file
93
tests/error_handler_test.py
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
|
import os.path
|
||||||
|
import mock
|
||||||
|
import pytest
|
||||||
|
import re
|
||||||
|
|
||||||
|
from pre_commit import error_handler
|
||||||
|
from pre_commit.errors import FatalError
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.yield_fixture
|
||||||
|
def mocked_log_and_exit():
|
||||||
|
with mock.patch.object(error_handler, '_log_and_exit') as log_and_exit:
|
||||||
|
yield log_and_exit
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_no_exception(mocked_log_and_exit):
|
||||||
|
with error_handler.error_handler():
|
||||||
|
pass
|
||||||
|
assert mocked_log_and_exit.call_count == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_fatal_error(mocked_log_and_exit):
|
||||||
|
exc = FatalError('just a test')
|
||||||
|
with error_handler.error_handler():
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
mocked_log_and_exit.assert_called_once_with(
|
||||||
|
'An error has occurred',
|
||||||
|
exc,
|
||||||
|
# Tested below
|
||||||
|
mock.ANY,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert re.match(
|
||||||
|
'Traceback \(most recent call last\):\n'
|
||||||
|
' File ".+/pre_commit/error_handler.py", line \d+, in error_handler\n'
|
||||||
|
' yield\n'
|
||||||
|
' File ".+/tests/error_handler_test.py", line \d+, '
|
||||||
|
'in test_error_handler_fatal_error\n'
|
||||||
|
' raise exc\n'
|
||||||
|
'(pre_commit\.errors\.)?FatalError: just a test\n',
|
||||||
|
mocked_log_and_exit.call_args[0][2],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_uncaught_error(mocked_log_and_exit):
|
||||||
|
exc = ValueError('another test')
|
||||||
|
with error_handler.error_handler():
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
mocked_log_and_exit.assert_called_once_with(
|
||||||
|
'An unexpected error has occurred',
|
||||||
|
exc,
|
||||||
|
# Tested below
|
||||||
|
mock.ANY,
|
||||||
|
)
|
||||||
|
assert re.match(
|
||||||
|
'Traceback \(most recent call last\):\n'
|
||||||
|
' File ".+/pre_commit/error_handler.py", line \d+, in error_handler\n'
|
||||||
|
' yield\n'
|
||||||
|
' File ".+/tests/error_handler_test.py", line \d+, '
|
||||||
|
'in test_error_handler_uncaught_error\n'
|
||||||
|
' raise exc\n'
|
||||||
|
'ValueError: another test\n',
|
||||||
|
mocked_log_and_exit.call_args[0][2],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_and_exit(mock_out_store_directory):
|
||||||
|
mocked_print = mock.Mock()
|
||||||
|
with pytest.raises(error_handler.PreCommitSystemExit):
|
||||||
|
error_handler._log_and_exit(
|
||||||
|
'msg', FatalError('hai'), "I'm a stacktrace",
|
||||||
|
print_fn=mocked_print,
|
||||||
|
)
|
||||||
|
|
||||||
|
printed = '\n'.join(call[0][0] for call in mocked_print.call_args_list)
|
||||||
|
assert printed == (
|
||||||
|
'msg: FatalError: hai\n'
|
||||||
|
'Check the log at ~/.pre-commit/pre-commit.log'
|
||||||
|
)
|
||||||
|
|
||||||
|
log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
|
||||||
|
assert os.path.exists(log_file)
|
||||||
|
contents = io.open(log_file).read()
|
||||||
|
assert contents == (
|
||||||
|
'msg: FatalError: hai\n'
|
||||||
|
"I'm a stacktrace\n"
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue