diff options
author | crupest <crupest@outlook.com> | 2024-11-11 01:12:29 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-01-16 21:14:14 +0800 |
commit | c0ba4d9d8d19d3faa7b4d2b3509546e37dd32364 (patch) | |
tree | 4b3d969850981094ba33e18ac9ec49fbc409dc93 /tools/cru-py/cru/_error.py | |
parent | 7f2e4107e7f469d6747350487e9441d9b987de47 (diff) | |
download | crupest-c0ba4d9d8d19d3faa7b4d2b3509546e37dd32364.tar.gz crupest-c0ba4d9d8d19d3faa7b4d2b3509546e37dd32364.tar.bz2 crupest-c0ba4d9d8d19d3faa7b4d2b3509546e37dd32364.zip |
HALF WORK: 2024.1.16
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!") |