diff options
Diffstat (limited to 'tools/cru-py/cru/_error.py')
-rw-r--r-- | tools/cru-py/cru/_error.py | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/tools/cru-py/cru/_error.py b/tools/cru-py/cru/_error.py index e4bf3d6..e53c787 100644 --- a/tools/cru-py/cru/_error.py +++ b/tools/cru-py/cru/_error.py @@ -1,29 +1,89 @@ -from typing import NoReturn +from __future__ import annotations + +from typing import NoReturn, cast, overload class CruException(Exception): """Base exception class of all exceptions in cru.""" + @overload + def __init__( + self, + message: None = None, + *args, + user_message: str, + **kwargs, + ): ... -class CruUnreachableError(CruException): - """Raised when a code path is unreachable.""" + @overload + def __init__( + self, + message: str, + *args, + user_message: str | None = None, + **kwargs, + ): ... + def __init__( + self, + message: str | None = None, + *args, + user_message: str | None = None, + **kwargs, + ): + if message is None: + message = user_message -def cru_unreachable() -> NoReturn: - raise CruUnreachableError() + super().__init__( + message, + *args, + **kwargs, + ) + self._message: str + self._message = cast(str, message) + self._user_message = user_message + @property + def message(self) -> str: + return self._message -class CruInternalError(CruException): - """Raised when an internal logic error occurs.""" + def get_user_message(self) -> str | None: + return self._user_message + def get_message(self, use_user: bool = True) -> str: + if use_user and self._user_message is not None: + return self._user_message + else: + return self._message -class CruUserFriendlyException(CruException): - def __init__( - self, message: str, user_message: str | None = None, *args, **kwargs - ) -> None: - super().__init__(message, *args, **kwargs) - self._user_message = user_message or message + @property + def is_internal(self) -> bool: + return False @property - def user_message(self) -> str: - return self._user_message + def is_logic_error(self) -> bool: + return False + + +class CruLogicError(CruException): + """Raised when a logic error occurs.""" + + @property + def is_logic_error(self) -> bool: + return True + + +class CruInternalError(CruException): + """Raised when an internal error occurs.""" + + @property + def is_internal(self) -> bool: + return True + + +class CruUnreachableError(CruInternalError): + """Raised when a code path is unreachable.""" + + +def cru_unreachable() -> NoReturn: + raise CruUnreachableError("Code should not reach here!") |