diff --git a/bin/cptemplate b/bin/cptemplate index 6eeb6d7..d5d9e87 100755 --- a/bin/cptemplate +++ b/bin/cptemplate @@ -72,7 +72,7 @@ if __name__ == "__main__": exit(error.E_INTERRUPT) except fzf.FZFError as f: print(f) - exit(error.E_INTERRUPT) + exit(f.exit_code) dest_file = Path(args.dest) diff --git a/bin/fedit b/bin/fedit index f6ffe7e..46fb86f 100755 --- a/bin/fedit +++ b/bin/fedit @@ -97,8 +97,9 @@ if __name__ == "__main__": selected_file = fzf.select_file_with_fzf(files) except KeyboardInterrupt: exit(error.E_INTERRUPT) - except fzf.FZFError: - exit(error.E_NOFILESELECTED) + except fzf.FZFError as e: + print(e) + exit(e.exit_code) if selected_file != "": cmd = editor.gen_editor_cmd(selected_editor, selected_file) diff --git a/file_scripts/fzf.py b/file_scripts/fzf.py index 8f24fdd..9492c5a 100644 --- a/file_scripts/fzf.py +++ b/file_scripts/fzf.py @@ -22,7 +22,28 @@ LOCALE = "utf-8" class FZFError(Exception): - pass + """Custom error class for any errors that occur when fzf is run. + + **Attributes** + + * exit_code: exit code when fzf exited + * message: stderr of fzf's process when the error occured + + """ + + def __init__(self, exit_code, message): + """ + :type exit_code: int + :type message: str + """ + self.exit_code = exit_code + self.message = message + + def __repr__(self): + return f"FZFError({self.exit_code}, {self.message})" + + def __str__(self): + return self.message # ========== Functions ========== @@ -40,6 +61,6 @@ def select_file_with_fzf(files): try: output.check_returncode() except subprocess.CalledProcessError as e: - raise FZFError(error.NO_FILE_SELECTED_MESSAGE) from e + raise FZFError(e.returncode, e.stderr) from e else: return Path(output.stdout.decode(LOCALE).strip("\x00")) diff --git a/tests/test_fzf.py b/tests/test_fzf.py index 876c334..1e82c57 100644 --- a/tests/test_fzf.py +++ b/tests/test_fzf.py @@ -25,3 +25,21 @@ class TestRunFZF(unittest.TestCase): def tearDown(self): patch.stopall() + + +class TestFZFErrors(unittest.TestCase): + def setUp(self): + self.patched_subprocess = patch(f"{TESTING_MODULE}.subprocess.run") + + 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") + ) + + with self.assertRaises(fzf.FZFError): + fzf.select_file_with_fzf(b"test") + + def tearDown(self): + patch.stopall()