Skip to content

Commit

Permalink
fix: sys.exit with errorcode when exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
qduanmu committed Dec 23, 2024
1 parent d701aab commit b04a6c2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions tests/trestlebot/cli/test_autosync_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_missing_markdown_dir_option(tmp_repo: Tuple[str, Repo]) -> None:
assert result.exit_code == 2
assert "Error: Missing option '--markdown-dir'" in result.output

# With 'markdown_dir' setting in config.yml
# With non-existent 'markdown_dir' setting in config.yml
config_obj = TrestleBotConfig(markdown_dir="markdown")
write_to_file(config_obj, filepath)
result = runner.invoke(autosync_cmd, cmd_options)
assert result.exit_code == 0
assert result.exit_code == 1
4 changes: 3 additions & 1 deletion tests/trestlebot/cli/test_sync_upstreams_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_sync_upstreams(tmp_repo: Tuple[str, Repo]) -> None:
"test@email",
"--committer-name",
"test name",
"--dry-run",
],
)

Expand Down Expand Up @@ -91,7 +92,7 @@ def test_sync_upstreams_with_config(
runner = CliRunner()
result = runner.invoke(
sync_upstreams_cmd,
["--repo-path", repo_path, "--config", config_path.resolve()],
["--repo-path", repo_path, "--config", config_path.resolve(), "--dry-run"],
)
assert repo_path.joinpath(TEST_CATALOG_PATH).exists()
assert repo_path.joinpath(TEST_PROFILE_PATH).exists()
Expand Down Expand Up @@ -123,6 +124,7 @@ def test_sync_upstreams_exclude_models(tmp_repo: Tuple[str, Repo]) -> None:
"test@email",
"--committer-name",
"test",
"--dry-run",
],
)

Expand Down
8 changes: 5 additions & 3 deletions trestlebot/cli/options/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from trestlebot.cli.config import TrestleBotConfigError, load_from_file
from trestlebot.cli.log import set_log_level
from trestlebot.const import ERROR_EXIT_CODE, TRESTLEBOT_CONFIG_DIR
from trestlebot.const import ERROR_EXIT_CODE, SUCCESS_EXIT_CODE, TRESTLEBOT_CONFIG_DIR


F = TypeVar("F", bound=Callable[..., Any])
Expand All @@ -27,12 +27,14 @@
def handle_exceptions(func: F) -> Any:
def wrapper(*args: Sequence[Any], **kwargs: Dict[Any, Any]) -> Any:
try:
return func(*args, **kwargs)
func(*args, **kwargs)
except Exception as ex:
traceback_str = traceback.format_exc()
logger.error(f"Trestle-bot Error: {str(ex)}")
logger.debug(traceback_str)
return ERROR_EXIT_CODE
sys.exit(ERROR_EXIT_CODE)

sys.exit(SUCCESS_EXIT_CODE)

return wrapper

Expand Down

0 comments on commit b04a6c2

Please sign in to comment.