mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-13 12:30:08 -06:00
Add error_handler and use it.
This commit is contained in:
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
|
||||
|
||||
"""five: six, redux"""
|
||||
# pylint:disable=invalid-name
|
||||
PY2 = str is bytes
|
||||
PY3 = str is not bytes
|
||||
|
||||
@@ -20,20 +20,20 @@ def extend_validator_cls(validator_cls, modify):
|
||||
|
||||
|
||||
def default_values(properties, instance):
|
||||
for property, subschema in properties.items():
|
||||
for prop, subschema in properties.items():
|
||||
if 'default' in subschema:
|
||||
instance.setdefault(
|
||||
property, copy.deepcopy(subschema['default']),
|
||||
prop, copy.deepcopy(subschema['default']),
|
||||
)
|
||||
|
||||
|
||||
def remove_default_values(properties, instance):
|
||||
for property, subschema in properties.items():
|
||||
for prop, subschema in properties.items():
|
||||
if (
|
||||
'default' in subschema and
|
||||
instance.get(property) == subschema['default']
|
||||
'default' in subschema and
|
||||
instance.get(prop) == subschema['default']
|
||||
):
|
||||
del instance[property]
|
||||
del instance[prop]
|
||||
|
||||
|
||||
_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 uninstall
|
||||
from pre_commit.commands.run import run
|
||||
from pre_commit.error_handler import error_handler
|
||||
from pre_commit.runner import Runner
|
||||
from pre_commit.util import entry
|
||||
|
||||
@@ -83,28 +84,29 @@ def main(argv):
|
||||
else:
|
||||
parser.parse_args(['--help'])
|
||||
|
||||
runner = Runner.create()
|
||||
with error_handler():
|
||||
runner = Runner.create()
|
||||
|
||||
if args.command == 'install':
|
||||
return install(
|
||||
runner, overwrite=args.overwrite, hooks=args.install_hooks,
|
||||
)
|
||||
elif args.command == 'uninstall':
|
||||
return uninstall(runner)
|
||||
elif args.command == 'clean':
|
||||
return clean(runner)
|
||||
elif args.command == 'autoupdate':
|
||||
return autoupdate(runner)
|
||||
elif args.command == 'run':
|
||||
return run(runner, args)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'Command {0} not implemented.'.format(args.command)
|
||||
)
|
||||
if args.command == 'install':
|
||||
return install(
|
||||
runner, overwrite=args.overwrite, hooks=args.install_hooks,
|
||||
)
|
||||
elif args.command == 'uninstall':
|
||||
return uninstall(runner)
|
||||
elif args.command == 'clean':
|
||||
return clean(runner)
|
||||
elif args.command == 'autoupdate':
|
||||
return autoupdate(runner)
|
||||
elif args.command == 'run':
|
||||
return run(runner, args)
|
||||
else:
|
||||
raise NotImplementedError(
|
||||
'Command {0} not implemented.'.format(args.command)
|
||||
)
|
||||
|
||||
raise AssertionError(
|
||||
'Command {0} failed to exit with a returncode'.format(args.command)
|
||||
)
|
||||
raise AssertionError(
|
||||
'Command {0} failed to exit with a returncode'.format(args.command)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
@@ -16,13 +16,13 @@ COLS = int(
|
||||
|
||||
|
||||
def get_hook_message(
|
||||
start,
|
||||
postfix='',
|
||||
end_msg=None,
|
||||
end_len=0,
|
||||
end_color=None,
|
||||
use_color=None,
|
||||
cols=COLS,
|
||||
start,
|
||||
postfix='',
|
||||
end_msg=None,
|
||||
end_len=0,
|
||||
end_color=None,
|
||||
use_color=None,
|
||||
cols=COLS,
|
||||
):
|
||||
"""Prints a message for running a hook.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user