diff options
| author | 2025-10-25 00:30:48 +0800 | |
|---|---|---|
| committer | 2025-10-25 00:30:48 +0800 | |
| commit | cbc653ffd0ea9abf4360623dc7a7651e1a49cc61 (patch) | |
| tree | ea3c396148158077bae3e77eaa9341f8c1990636 /src/conventionalrp/utils/exceptions.py | |
| parent | 08299b37dfda86e56e4f2b442f68ccd2da7a82e3 (diff) | |
| download | conventional_role_play-cbc653ffd0ea9abf4360623dc7a7651e1a49cc61.tar.gz conventional_role_play-cbc653ffd0ea9abf4360623dc7a7651e1a49cc61.zip | |
feat: Implement plugin system with combat tracker and dice analyzer
- Added `plugin_system_demo.py` to demonstrate basic plugin usage, processing, and analysis.
- Created `CombatTrackerPlugin` for tracking combat statistics including damage and healing.
- Developed `DiceAnalyzerPlugin` for analyzing dice rolls and calculating success rates.
- Introduced `renderer_demo.py` for rendering output in HTML, Markdown, and JSON formats.
- Implemented `rule_system_demo.py` to showcase rule engine capabilities with various examples.
- Established core rule engine functionality in `rules.py` with support for conditions and actions.
- Enhanced base plugin structure in `base.py` to support different plugin types (Processor, Renderer, Analyzer).
- Added custom exception handling in `exceptions.py` for better error management.
- Configured logging setup in `logging_config.py` for improved logging capabilities.
- Created unit tests in `test_rust_core.py` to validate core functionalities and performance.
Diffstat (limited to 'src/conventionalrp/utils/exceptions.py')
| -rw-r--r-- | src/conventionalrp/utils/exceptions.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/conventionalrp/utils/exceptions.py b/src/conventionalrp/utils/exceptions.py new file mode 100644 index 0000000..28fa179 --- /dev/null +++ b/src/conventionalrp/utils/exceptions.py @@ -0,0 +1,104 @@ +from typing import Optional, Any, Dict +import traceback +import logging + +logger = logging.getLogger(__name__) + + +class ConventionalRPError(Exception): + """基础异常类""" + + def __init__( + self, + message: str, + details: Optional[Dict[str, Any]] = None, + cause: Optional[Exception] = None + ): + super().__init__(message) + self.message = message + self.details = details or {} + self.cause = cause + + def __str__(self) -> str: + result = self.message + if self.details: + details_str = ", ".join(f"{k}={v}" for k, v in self.details.items()) + result += f" ({details_str})" + if self.cause: + result += f"\nCaused by: {str(self.cause)}" + return result + + +class ParserError(ConventionalRPError): + """解析错误""" + pass + + +class RuleError(ConventionalRPError): + """规则相关错误""" + pass + + +class ProcessorError(ConventionalRPError): + """处理器错误""" + pass + + +class ValidationError(ConventionalRPError): + """验证错误""" + pass + + +class ConfigurationError(ConventionalRPError): + """配置错误""" + pass + + +def safe_execute(func, *args, default=None, error_msg="Operation failed", **kwargs): + try: + return func(*args, **kwargs) + except Exception as e: + logger.error(f"{error_msg}: {e}") + return default + + +def format_error(error: Exception, include_traceback: bool = False) -> str: + error_type = type(error).__name__ + error_msg = str(error) + + result = f"[{error_type}] {error_msg}" + + if include_traceback: + tb = traceback.format_exc() + result += f"\n\nTraceback:\n{tb}" + + return result + + +def validate_not_none(value: Any, name: str): + if value is None: + raise ValidationError( + f"{name} cannot be None", + details={"parameter": name} + ) + + +def validate_type(value: Any, expected_type: type, name: str): + if not isinstance(value, expected_type): + raise ValidationError( + f"{name} must be of type {expected_type.__name__}, " + f"got {type(value).__name__}", + details={ + "parameter": name, + "expected_type": expected_type.__name__, + "actual_type": type(value).__name__ + } + ) + + +def validate_not_empty(value: Any, name: str): + if not value: + raise ValidationError( + f"{name} cannot be empty", + details={"parameter": name, "value": value} + ) |
