Merge pull request #50 from kiviktnm/feat/print-failed-command-exit-code

Include exit code in CommandFailedError
This commit is contained in:
Kivi Kaitaniemi
2026-02-07 13:26:01 +02:00
committed by GitHub
4 changed files with 11 additions and 6 deletions
@@ -195,7 +195,7 @@ def test_apply_returns_false_on_command_failure(monkeypatch: pytest.MonkeyPatch)
pass
def get_native_explicit(self) -> set[str]:
raise pacman_plugin.errors.CommandFailedError(["get_native_explicit"], "boom")
raise pacman_plugin.errors.CommandFailedError(["get_native_explicit"], 10, "boom")
monkeypatch.setattr(pacman_plugin, "PacmanInterface", FailingPM)
+2 -2
View File
@@ -218,9 +218,9 @@ def check_run_result(
code, output = result
if code != 0:
if include_output:
raise errors.CommandFailedError(command, output)
raise errors.CommandFailedError(command, code, output)
else:
raise errors.CommandFailedError(command, None)
raise errors.CommandFailedError(command, code, None)
return code, output
+6 -2
View File
@@ -74,13 +74,17 @@ class CommandFailedError(Exception):
Attributes:
command (list[str]): The command that caused the exception.
exit_code (int): The exit code of the command
output (str|None): Output of the command.
"""
def __init__(self, command: list[str], output: str | None) -> None:
def __init__(self, command: list[str], exit_code: int, output: str | None) -> None:
self.command = shlex.join(command)
self.exit_code = exit_code
if output:
self.output: str | None = output.strip()
else:
self.output = None
super().__init__(f"Command '{self.command}' returned with a non-zero exit code.")
super().__init__(
f"Command '{self.command}' returned with a non-zero exit code {self.exit_code}."
)
+2 -1
View File
@@ -100,6 +100,7 @@ class _GPGInterface:
"--no-tty",
"--import-ownertrust",
]
# use subprocess manually since decman exposed functions don't allow setting input
p = subprocess.run(
cmd,
input=data,
@@ -110,7 +111,7 @@ class _GPGInterface:
check=False,
)
if p.returncode != 0:
raise CommandFailedError(cmd, p.stdout)
raise CommandFailedError(cmd, p.returncode, p.stdout)
def delete_keys(self, fingerprints: list[str]):
decman.prg(