errors.py
python
| 1 | """Exit-code contract and exception types for the Muse CLI.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import enum |
| 6 | |
| 7 | |
| 8 | class ExitCode(enum.IntEnum): |
| 9 | """Standardised CLI exit codes. |
| 10 | |
| 11 | 0 — success |
| 12 | 1 — user error (bad arguments, invalid input) |
| 13 | 2 — repo-not-found / config invalid |
| 14 | 3 — server / internal error |
| 15 | """ |
| 16 | |
| 17 | SUCCESS = 0 |
| 18 | USER_ERROR = 1 |
| 19 | REPO_NOT_FOUND = 2 |
| 20 | INTERNAL_ERROR = 3 |
| 21 | |
| 22 | |
| 23 | class MuseCLIError(Exception): |
| 24 | """Base exception for Muse CLI errors.""" |
| 25 | |
| 26 | def __init__(self, message: str, exit_code: ExitCode = ExitCode.INTERNAL_ERROR) -> None: |
| 27 | super().__init__(message) |
| 28 | self.exit_code = exit_code |
| 29 | |
| 30 | |
| 31 | class RepoNotFoundError(MuseCLIError): |
| 32 | """Raised when the current directory is not a Muse repository.""" |
| 33 | |
| 34 | def __init__(self, message: str = "Not a Muse repository. Run `muse init`.") -> None: |
| 35 | super().__init__(message, exit_code=ExitCode.REPO_NOT_FOUND) |
| 36 | |
| 37 | |
| 38 | #: Canonical public alias matching the name specified. |
| 39 | MuseNotARepoError = RepoNotFoundError |