refactor(cli): improve error handling and type hints in utils
- Change _exit_with_config_error return type to NoReturn for clarity - Replace exit() with raise SystemExit() for better testability - Remove unreachable return statement after error exit
This commit is contained in:
parent
4261c62e7c
commit
4c5f0d8ae4
|
|
@ -1,6 +1,6 @@
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List, NoReturn
|
||||||
|
|
||||||
import questionary
|
import questionary
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
@ -11,9 +11,10 @@ console = Console()
|
||||||
|
|
||||||
CONFIG_PATH = Path(__file__).resolve().parents[1] / "config.json"
|
CONFIG_PATH = Path(__file__).resolve().parents[1] / "config.json"
|
||||||
|
|
||||||
def _exit_with_config_error(message: str) -> None:
|
def _exit_with_config_error(message: str) -> NoReturn:
|
||||||
|
"""Exit with error message. This function never returns."""
|
||||||
console.print(f"\n[red]{message}[/red]")
|
console.print(f"\n[red]{message}[/red]")
|
||||||
exit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
|
||||||
def _load_config() -> dict:
|
def _load_config() -> dict:
|
||||||
|
|
@ -26,7 +27,6 @@ def _load_config() -> dict:
|
||||||
_exit_with_config_error(f"Invalid JSON in config file: {exc}")
|
_exit_with_config_error(f"Invalid JSON in config file: {exc}")
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
_exit_with_config_error(f"Unable to read config file: {exc}")
|
_exit_with_config_error(f"Unable to read config file: {exc}")
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def _get_config_section(config: dict, key: str, expected_type: type):
|
def _get_config_section(config: dict, key: str, expected_type: type):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue