| 1234567891011121314151617 |
- from functools import wraps
- from flask import jsonify
- import traceback
- def handle_exception(f):
- """异常处理装饰器"""
- @wraps(f)
- def decorated_function(*args, **kwargs):
- try:
- return f(*args, **kwargs)
- except Exception as e:
- print(f"\n❌ [装饰器] 捕获到异常: {str(e)}")
- print(f"🔴 [装饰器] 错误类型: {type(e).__name__}")
- print("🔴 [装饰器] 错误堆栈:")
- print(traceback.format_exc())
- return jsonify({'error': f'服务器内部错误: {str(e)}'}), 500
- return decorated_function
|