Skip to content

Commit 042e0ae

Browse files
authored
fix(cli): catch CommandError to show clean error instead of traceback (#1182)
When a git command fails (e.g., rebase conflict during `mergify stack push`), the full Python traceback was displayed. Now CommandError is caught at the top level in main() and printed as a clean one-line error message, matching how httpx.HTTPStatusError is already handled. Uses structured exit codes from the exit code system.
1 parent 7adb94b commit 042e0ae

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

mergify_cli/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import httpx
2424

2525
from mergify_cli import VERSION
26+
from mergify_cli import console
2627
from mergify_cli import utils
2728
from mergify_cli.ci import cli as ci_cli_mod
2829
from mergify_cli.config import cli as config_cli_mod
@@ -86,3 +87,6 @@ def main() -> None:
8687
if str(e.request.url).startswith(utils.get_mergify_api_url()):
8788
raise SystemExit(ExitCode.MERGIFY_API_ERROR) from None
8889
raise SystemExit(ExitCode.GITHUB_API_ERROR) from None
90+
except utils.CommandError as e:
91+
console.print(f"error: {e}", style="red")
92+
raise SystemExit(ExitCode.GENERIC_ERROR) from None

mergify_cli/tests/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
from __future__ import annotations
22

3+
from unittest import mock
4+
35
from click import testing
6+
import pytest
47

58
from mergify_cli import cli as cli_mod
9+
from mergify_cli import utils
10+
from mergify_cli.exit_codes import ExitCode
11+
12+
13+
def test_cli_command_error_shows_clean_message() -> None:
14+
"""Test that CommandError produces a clean error message, not a traceback."""
15+
error = utils.CommandError(
16+
("git", "pull", "--rebase", "origin", "main"),
17+
1,
18+
b"CONFLICT (content): Merge conflict in file.txt",
19+
)
20+
with (
21+
mock.patch.object(cli_mod, "cli", side_effect=error),
22+
pytest.raises(SystemExit) as exc_info,
23+
):
24+
cli_mod.main()
25+
26+
assert exc_info.value.code == ExitCode.GENERIC_ERROR
627

728

829
def test_cli_shows_help_by_default() -> None:

0 commit comments

Comments
 (0)