feat: 实现统一的错误处理机制和增强日志系统
添加错误码定义和统一响应格式 增强日志记录功能,支持模块专用日志记录器 实现全局异常捕获和友好错误提示 更新文档说明错误处理机制
This commit is contained in:
57
main.py
57
main.py
@@ -128,8 +128,8 @@ class PluginReloadHandler(FileSystemEventHandler):
|
||||
if not src_path.endswith(".py"):
|
||||
return
|
||||
|
||||
# 过滤掉一些临时文件
|
||||
if "__pycache__" in src_path or not src_path.startswith(PLUGIN_DIR):
|
||||
# 过滤掉一些临时文件和__init__.py文件
|
||||
if "__pycache__" in src_path or not src_path.startswith(PLUGIN_DIR) or os.path.basename(src_path) == "__init__.py":
|
||||
return
|
||||
|
||||
# 简单的防抖动
|
||||
@@ -216,4 +216,55 @@ async def main():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
"""
|
||||
程序主入口,添加全局异常捕获和友好提示
|
||||
"""
|
||||
from core.utils.error_codes import exception_to_error_response
|
||||
from core.utils.logger import ModuleLogger
|
||||
|
||||
# 创建主程序日志记录器
|
||||
main_logger = ModuleLogger("Main")
|
||||
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except KeyboardInterrupt:
|
||||
main_logger.info("程序已被用户中断")
|
||||
exit(0)
|
||||
except Exception as e:
|
||||
main_logger.exception("程序发生未处理的全局异常")
|
||||
|
||||
# 生成统一的错误响应
|
||||
error_response = exception_to_error_response(e)
|
||||
|
||||
# 打印友好的错误提示
|
||||
print("\n" + "=" * 60)
|
||||
print("程序发生错误,请检查以下信息:")
|
||||
print("=" * 60)
|
||||
print(f"错误代码: {error_response['code']}")
|
||||
print(f"错误信息: {error_response['message']}")
|
||||
print("=" * 60)
|
||||
print("详细错误信息已记录到日志文件中")
|
||||
print("请检查 logs 目录下的日志文件以获取更多调试信息")
|
||||
print("=" * 60)
|
||||
|
||||
# 根据错误类型给出不同的建议
|
||||
if hasattr(e, "original_error") and e.original_error:
|
||||
print(f"\n原始错误: {e.original_error}")
|
||||
|
||||
if "WebSocket" in str(type(e).__name__):
|
||||
print("\n建议检查:")
|
||||
print("1. WebSocket 服务是否正在运行")
|
||||
print("2. 配置文件中的 WebSocket 地址和令牌是否正确")
|
||||
print("3. 网络连接是否正常")
|
||||
elif "Config" in str(type(e).__name__):
|
||||
print("\n建议检查:")
|
||||
print("1. 配置文件 config.toml 是否存在")
|
||||
print("2. 配置文件格式是否正确")
|
||||
print("3. 所有必填配置项是否都已设置")
|
||||
elif "Plugin" in str(type(e).__name__):
|
||||
print("\n建议检查:")
|
||||
print("1. 插件目录是否存在")
|
||||
print("2. 插件文件是否有语法错误")
|
||||
print("3. 插件是否符合插件开发规范")
|
||||
|
||||
exit(1)
|
||||
|
||||
Reference in New Issue
Block a user