From 63fad53303381388673073de580a32088a4ef0fe Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 24 Mar 2020 22:59:15 +0100 Subject: Adding upstream version 2.2.0. Signed-off-by: Daniel Baumann --- pre_commit/error_handler.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 pre_commit/error_handler.py (limited to 'pre_commit/error_handler.py') diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py new file mode 100644 index 0000000..b2321ae --- /dev/null +++ b/pre_commit/error_handler.py @@ -0,0 +1,64 @@ +import contextlib +import functools +import os.path +import sys +import traceback +from typing import Generator + +import pre_commit.constants as C +from pre_commit import output +from pre_commit.store import Store +from pre_commit.util import force_bytes + + +class FatalError(RuntimeError): + pass + + +def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None: + error_msg = f'{msg}: {type(exc).__name__}: '.encode() + force_bytes(exc) + output.write_line_b(error_msg) + log_path = os.path.join(Store().directory, 'pre-commit.log') + output.write_line(f'Check the log at {log_path}') + + with open(log_path, 'wb') as log: + _log_line = functools.partial(output.write_line, stream=log) + _log_line_b = functools.partial(output.write_line_b, stream=log) + + _log_line('### version information') + _log_line() + _log_line('```') + _log_line(f'pre-commit version: {C.VERSION}') + _log_line('sys.version:') + for line in sys.version.splitlines(): + _log_line(f' {line}') + _log_line(f'sys.executable: {sys.executable}') + _log_line(f'os.name: {os.name}') + _log_line(f'sys.platform: {sys.platform}') + _log_line('```') + _log_line() + + _log_line('### error information') + _log_line() + _log_line('```') + _log_line_b(error_msg) + _log_line('```') + _log_line() + _log_line('```') + _log_line(formatted) + _log_line('```') + raise SystemExit(1) + + +@contextlib.contextmanager +def error_handler() -> Generator[None, None, None]: + try: + yield + except (Exception, KeyboardInterrupt) as e: + if isinstance(e, FatalError): + msg = 'An error has occurred' + elif isinstance(e, KeyboardInterrupt): + msg = 'Interrupted (^C)' + else: + msg = 'An unexpected error has occurred' + _log_and_exit(msg, e, traceback.format_exc()) -- cgit v1.2.3