decorators.py 610 B

1234567891011121314151617
  1. from functools import wraps
  2. from flask import jsonify
  3. import traceback
  4. def handle_exception(f):
  5. """异常处理装饰器"""
  6. @wraps(f)
  7. def decorated_function(*args, **kwargs):
  8. try:
  9. return f(*args, **kwargs)
  10. except Exception as e:
  11. print(f"\n❌ [装饰器] 捕获到异常: {str(e)}")
  12. print(f"🔴 [装饰器] 错误类型: {type(e).__name__}")
  13. print("🔴 [装饰器] 错误堆栈:")
  14. print(traceback.format_exc())
  15. return jsonify({'error': f'服务器内部错误: {str(e)}'}), 500
  16. return decorated_function