"""飞书API错误处理"""
import functools
import lark_oapi as lark
from typing import Callable
class FeishuError(Exception):
"""飞书API错误"""
pass
def handle_feishu_error(func: Callable) -> Callable:
"""
错误处理装饰器
统一处理飞书API错误,返回序列化的JSON字符串
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
response = func(*args, **kwargs)
if not response.success():
raise FeishuError(
f"API调用失败: {response.msg} "
f"(错误码: {response.code}, 日志ID: {response.get_log_id()})"
)
# 序列化并返回data部分
return lark.JSON.marshal(response.data, indent=4)
return wrapper