mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-20 01:24:42 +04:00
Merge pull request #1359 from pre-commit/bytesable_exceptions
Don't crash on un-stringable exceptions
This commit is contained in:
commit
1c10340943
2 changed files with 30 additions and 3 deletions
|
|
@ -14,14 +14,24 @@ class FatalError(RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _exception_to_bytes(exc: BaseException) -> bytes:
|
||||||
|
with contextlib.suppress(TypeError):
|
||||||
|
return bytes(exc) # type: ignore
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
return str(exc).encode()
|
||||||
|
return f'<unprintable {type(exc).__name__} object>'.encode()
|
||||||
|
|
||||||
|
|
||||||
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
||||||
error_msg = f'{msg}: {type(exc).__name__}: {exc}'
|
error_msg = f'{msg}: {type(exc).__name__}: '.encode()
|
||||||
output.write_line(error_msg)
|
error_msg += _exception_to_bytes(exc)
|
||||||
|
output.write_line_b(error_msg)
|
||||||
log_path = os.path.join(Store().directory, 'pre-commit.log')
|
log_path = os.path.join(Store().directory, 'pre-commit.log')
|
||||||
output.write_line(f'Check the log at {log_path}')
|
output.write_line(f'Check the log at {log_path}')
|
||||||
|
|
||||||
with open(log_path, 'wb') as log:
|
with open(log_path, 'wb') as log:
|
||||||
_log_line = functools.partial(output.write_line, stream=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('### version information')
|
||||||
_log_line()
|
_log_line()
|
||||||
|
|
@ -39,7 +49,7 @@ def _log_and_exit(msg: str, exc: BaseException, formatted: str) -> None:
|
||||||
_log_line('### error information')
|
_log_line('### error information')
|
||||||
_log_line()
|
_log_line()
|
||||||
_log_line('```')
|
_log_line('```')
|
||||||
_log_line(error_msg)
|
_log_line_b(error_msg)
|
||||||
_log_line('```')
|
_log_line('```')
|
||||||
_log_line()
|
_log_line()
|
||||||
_log_line('```')
|
_log_line('```')
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ from unittest import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit import error_handler
|
from pre_commit import error_handler
|
||||||
|
from pre_commit.util import CalledProcessError
|
||||||
from testing.util import cmd_output_mocked_pre_commit_home
|
from testing.util import cmd_output_mocked_pre_commit_home
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -135,6 +136,22 @@ def test_error_handler_non_ascii_exception(mock_store_dir):
|
||||||
raise ValueError('☃')
|
raise ValueError('☃')
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_non_utf8_exception(mock_store_dir):
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
with error_handler.error_handler():
|
||||||
|
raise CalledProcessError(1, ('exe',), 0, b'error: \xa0\xe1', b'')
|
||||||
|
|
||||||
|
|
||||||
|
def test_error_handler_non_stringable_exception(mock_store_dir):
|
||||||
|
class C(Exception):
|
||||||
|
def __str__(self):
|
||||||
|
raise RuntimeError('not today!')
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
with error_handler.error_handler():
|
||||||
|
raise C()
|
||||||
|
|
||||||
|
|
||||||
def test_error_handler_no_tty(tempdir_factory):
|
def test_error_handler_no_tty(tempdir_factory):
|
||||||
pre_commit_home = tempdir_factory.get()
|
pre_commit_home = tempdir_factory.get()
|
||||||
ret, out, _ = cmd_output_mocked_pre_commit_home(
|
ret, out, _ = cmd_output_mocked_pre_commit_home(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue