Add error message parameter for FZFError

This commit is contained in:
Eric Torres 2022-04-06 21:03:43 -07:00
parent 488a6be70c
commit 23d077bff1
2 changed files with 6 additions and 7 deletions

View File

@ -30,14 +30,15 @@ class FZFError(Exception):
"""
def __init__(self, exit_code):
def __init__(self, exit_code, message):
"""
:type exit_code: int
"""
self.exit_code = exit_code
self.message = message
def __repr__(self):
return f"FZFError({self.exit_code})"
return f"FZFError({self.exit_code}, {self.message})"
def __str__(self):
return str(self.__repr__())
@ -58,6 +59,6 @@ def select_file_with_fzf(files):
try:
output.check_returncode()
except subprocess.CalledProcessError as e:
raise FZFError(e.returncode) from e
raise FZFError(e.returncode, e.stderr) from e
else:
return Path(output.stdout.decode(LOCALE).strip("\x00"))

View File

@ -34,9 +34,7 @@ class TestFZFErrors(unittest.TestCase):
self.mocked_run = self.patched_subprocess.start()
def test_raises_error(self):
self.mocked_run.return_value.check_returncode.side_effect = (
subprocess.CalledProcessError(1, "hi")
)
self.mocked_run.side_effect = subprocess.CalledProcessError(1, "hi")
with self.assertRaises(fzf.FZFError):
fzf.select_file_with_fzf(b"test")
@ -47,6 +45,6 @@ class TestFZFErrors(unittest.TestCase):
class TestFZFErrorClass(unittest.TestCase):
def test_class_repr(self):
f = fzf.FZFError(1)
f = fzf.FZFError(1, "error")
f
str(f)