feat: 实现统一的错误处理机制和增强日志系统

添加错误码定义和统一响应格式
增强日志记录功能,支持模块专用日志记录器
实现全局异常捕获和友好错误提示
更新文档说明错误处理机制
This commit is contained in:
2026-01-19 14:05:14 +08:00
parent 698240b1a2
commit 8beeaef424
14 changed files with 1074 additions and 364 deletions

View File

@@ -135,11 +135,15 @@ def test_reload_plugin_error(plugin_manager):
plugin_manager.loaded_plugins.add(full_name)
mock_module = MagicMock()
# 创建一个模拟的logger直接替换plugin_manager实例的logger属性
mock_logger = MagicMock()
plugin_manager.logger = mock_logger
with patch.dict("sys.modules", {full_name: mock_module}), \
patch("importlib.reload", side_effect=Exception("Reload error")), \
patch("core.managers.plugin_manager.logger") as mock_logger:
patch("importlib.reload", side_effect=Exception("Reload error")):
# Should not raise exception
plugin_manager.reload_plugin(full_name)
mock_logger.exception.assert_called()
mock_logger.log_custom_exception.assert_called()

View File

@@ -37,14 +37,18 @@ class TestWS:
# 测试 WebSocket 未初始化的情况
result = await ws.call_api("send_group_msg", {"group_id": 123456, "message": "test"})
assert result == {"status": "failed", "msg": "websocket not initialized"}
assert result["code"] == 2002 # WS_DISCONNECTED
assert result["success"] == False
assert "WebSocket未初始化" in result["message"]
# 测试 WebSocket 已初始化但未连接的情况
mock_ws = MagicMock()
mock_ws.state = None
ws.ws = mock_ws
result = await ws.call_api("send_group_msg", {"group_id": 123456, "message": "test"})
assert result == {"status": "failed", "msg": "websocket is not open"}
assert result["code"] == 2002 # WS_DISCONNECTED
assert result["success"] == False
assert "WebSocket连接未打开" in result["message"]
@pytest.mark.asyncio
async def test_on_event_bot_initialization(self):