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/service/_base.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/service/_base.py')
-rw-r--r-- | tools/cru-py/cru/service/_base.py | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/tools/cru-py/cru/service/_base.py b/tools/cru-py/cru/service/_base.py index 124acd5..4454c2c 100644 --- a/tools/cru-py/cru/service/_base.py +++ b/tools/cru-py/cru/service/_base.py @@ -7,17 +7,27 @@ import os from pathlib import Path from typing import TypeVar, overload -from cru import CruException, CruInternalError, CruPath, CruUserFriendlyException +from cru import CruException, CruLogicError, CruPath _Feature = TypeVar("_Feature", bound="AppFeatureProvider") OWNER_NAME = "crupest" -class InternalAppException(CruInternalError): +class AppError(CruException): pass +class AppFeatureError(AppError): + def __init__(self, message, feature: type | str, *args, **kwargs): + super().__init__(message, *args, **kwargs) + self._feature = feature + + @property + def feature(self) -> type | str: + return self._feature + + class AppPathError(CruException): def __init__(self, message, _path: str | Path, *args, **kwargs): super().__init__(message, *args, **kwargs) @@ -152,12 +162,12 @@ class AppRootPath(AppPath): @property def full_path(self) -> CruPath: if self._full_path is None: - raise CruInternalError("App root path is not set yet.") + raise AppError("App root path is not set yet.") return self._full_path def setup(self, path: os.PathLike) -> None: if self._full_path is not None: - raise CruInternalError("App root path is already set.") + raise AppError("App root path is already set.") self._full_path = CruPath(path) @@ -270,7 +280,7 @@ class AppBase: @staticmethod def get_instance() -> AppBase: if AppBase._instance is None: - raise CruInternalError("App instance not initialized") + raise AppError("App instance not initialized") return AppBase._instance def __init__(self, name: str): @@ -312,8 +322,9 @@ class AppBase: def ensure_app_initialized(self) -> AppRootPath: if not self.app_initialized: - raise CruUserFriendlyException( - "Root directory does not exist. Please run 'init' to create one." + raise AppError( + user_message="Root directory does not exist. " + "Please run 'init' to create one." ) return self.root @@ -328,7 +339,9 @@ class AppBase: def add_feature(self, feature: _Feature) -> _Feature: for f in self.features: if f.name == feature.name: - raise CruInternalError(f"Duplicate feature name: {feature.name}.") + raise AppFeatureError( + f"Duplicate feature name: {feature.name}.", feature.name + ) self._features.append(feature) return feature @@ -365,14 +378,12 @@ class AppBase: if isinstance(f, feature): return f else: - raise InternalAppException( - "Argument must be the name of feature or its class." - ) + raise CruLogicError("Argument must be the name of feature or its class.") - raise InternalAppException(f"Feature {feature} not found.") + raise AppFeatureError(f"Feature {feature} not found.", feature) def get_path(self, name: str) -> AppFeaturePath: for p in self._paths: if p.id == name or p.name == name: return p - raise InternalAppException(f"Application path {name} not found.") + raise AppPathError(f"Application path {name} not found.", name) |