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 :type exit_code: int
""" """
self.exit_code = exit_code self.exit_code = exit_code
self.message = message
def __repr__(self): def __repr__(self):
return f"FZFError({self.exit_code})" return f"FZFError({self.exit_code}, {self.message})"
def __str__(self): def __str__(self):
return str(self.__repr__()) return str(self.__repr__())
@ -58,6 +59,6 @@ def select_file_with_fzf(files):
try: try:
output.check_returncode() output.check_returncode()
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
raise FZFError(e.returncode) from e raise FZFError(e.returncode, e.stderr) from e
else: else:
return Path(output.stdout.decode(LOCALE).strip("\x00")) 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() self.mocked_run = self.patched_subprocess.start()
def test_raises_error(self): def test_raises_error(self):
self.mocked_run.return_value.check_returncode.side_effect = ( self.mocked_run.side_effect = subprocess.CalledProcessError(1, "hi")
subprocess.CalledProcessError(1, "hi")
)
with self.assertRaises(fzf.FZFError): with self.assertRaises(fzf.FZFError):
fzf.select_file_with_fzf(b"test") fzf.select_file_with_fzf(b"test")
@ -47,6 +45,6 @@ class TestFZFErrors(unittest.TestCase):
class TestFZFErrorClass(unittest.TestCase): class TestFZFErrorClass(unittest.TestCase):
def test_class_repr(self): def test_class_repr(self):
f = fzf.FZFError(1) f = fzf.FZFError(1, "error")
f f
str(f) str(f)