stock-ai-agent/backend/test_exception_handler.py
2026-02-27 09:54:17 +08:00

54 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
测试全局异常处理器
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import asyncio
from app.utils.error_handler import setup_global_exception_handler, init_error_notifier
from app.services.feishu_service import get_feishu_service
async def test_exception_handler():
"""测试异常处理器"""
print("=" * 60)
print("测试全局异常处理器")
print("=" * 60)
print()
# 初始化
setup_global_exception_handler()
print("✅ 全局异常处理器已安装")
print()
# 初始化飞书通知
try:
feishu = get_feishu_service()
init_error_notifier(feishu_service=feishu, enabled=True, cooldown=10)
print("✅ 飞书错误通知已启用冷却时间10秒")
except Exception as e:
print(f"❌ 飞书错误通知初始化失败: {e}")
return
print()
# 模拟一个未捕获的异常
print("-" * 60)
print("将触发一个测试异常...")
print("-" * 60)
print()
# 这个异常会被全局异常处理器捕获
# 注意:这会触发飞书通知
raise ValueError("这是一个测试异常,用于验证全局异常处理器是否正常工作")
if __name__ == "__main__":
try:
asyncio.run(test_exception_handler())
except SystemExit:
# 异常被处理后,程序可能会退出
pass
print()
print("测试结束")