- Updated Codec class to use abstract methods for encoding and decoding envelopes. - Changed Envelope class to use Dict and Optional for payload and error fields. - Refined error handling in RPCError class with Optional type hints for details. - Enhanced manifest validation logic with type hints for better type safety. - Improved plugin loading mechanism with consistent type annotations. - Updated RPCClient to utilize Optional for codec and connection attributes. - Refactored transport classes to use Optional for server attributes and socket paths.
63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
"""RPC 错误码定义
|
|
|
|
所有 Host 与 Runner 之间的 RPC 通信使用统一的错误码体系。
|
|
"""
|
|
|
|
from enum import Enum
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
class ErrorCode(str, Enum):
|
|
"""RPC 错误码枚举"""
|
|
|
|
# 通用
|
|
OK = "OK"
|
|
E_UNKNOWN = "E_UNKNOWN"
|
|
|
|
# 协议层
|
|
E_TIMEOUT = "E_TIMEOUT"
|
|
E_BAD_PAYLOAD = "E_BAD_PAYLOAD"
|
|
E_PROTOCOL_MISMATCH = "E_PROTOCOL_MISMATCH"
|
|
|
|
# 权限与策略
|
|
E_UNAUTHORIZED = "E_UNAUTHORIZED"
|
|
E_METHOD_NOT_ALLOWED = "E_METHOD_NOT_ALLOWED"
|
|
E_BACKPRESSURE = "E_BACKPRESSURE"
|
|
E_HOST_OVERLOADED = "E_HOST_OVERLOADED"
|
|
|
|
# 插件生命周期
|
|
E_PLUGIN_CRASHED = "E_PLUGIN_CRASHED"
|
|
E_PLUGIN_NOT_FOUND = "E_PLUGIN_NOT_FOUND"
|
|
E_GENERATION_MISMATCH = "E_GENERATION_MISMATCH"
|
|
E_RELOAD_IN_PROGRESS = "E_RELOAD_IN_PROGRESS"
|
|
|
|
# 能力调用
|
|
E_CAPABILITY_DENIED = "E_CAPABILITY_DENIED"
|
|
E_CAPABILITY_FAILED = "E_CAPABILITY_FAILED"
|
|
|
|
|
|
class RPCError(Exception):
|
|
"""RPC 调用异常"""
|
|
|
|
def __init__(self, code: ErrorCode, message: str = "", details: Optional[Dict[str, Any]] = None):
|
|
self.code = code
|
|
self.message = message or code.value
|
|
self.details = details or {}
|
|
super().__init__(f"[{code.value}] {self.message}")
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
return {
|
|
"code": self.code.value,
|
|
"message": self.message,
|
|
"details": self.details,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: Dict[str, Any]) -> "RPCError":
|
|
code = ErrorCode(data.get("code", "E_UNKNOWN"))
|
|
return cls(
|
|
code=code,
|
|
message=data.get("message", ""),
|
|
details=data.get("details", {}),
|
|
)
|