from __future__ import annotations from typing import Any WECOM_ERROR_CODES: dict[int, str] = { -1: "系统繁忙,请稍后重试", 0: "请求成功", 40001: "不合法的secret参数", 40003: "不合法的UserID", 40014: "不合法的access_token", 40029: "不合法的oauth_code", 41001: "缺少access_token参数", 41002: "缺少appid参数", 42001: "access_token已过期", 43004: "需要接收者关注", 45009: "接口调用超过限制", 45011: "API调用过于频繁", 48001: "API功能未授权", 60011: "不合法的UserID列表", } MP_ERROR_CODES: dict[int, str] = { -1: "系统繁忙,请稍后重试", 0: "请求成功", 40001: "获取access_token时AppSecret错误", 40002: "不合法的凭证类型", 40003: "不合法的OpenID", 40013: "不合法的AppID", 40014: "不合法的access_token", 41001: "缺少access_token参数", 41002: "缺少appid参数", 42001: "access_token超时", 43004: "需要接收者关注", 45009: "接口调用超过限制", 45015: "回复时间超过限制", 48001: "API功能未授权", } def parse_wecom_error(data: dict[str, Any]) -> tuple[int, str]: if "errcode" not in data: return (-1, data.get("errmsg") or "未知错误(err_code 缺失)") errcode = data.get("errcode", -1) errmsg = data.get("errmsg", "") detail = WECOM_ERROR_CODES.get(errcode, errmsg or f"未知错误(err_code={errcode})") return errcode, detail def parse_mp_error(data: dict[str, Any]) -> tuple[int, str]: if "errcode" not in data: return (-1, data.get("errmsg") or "未知错误(err_code 缺失)") errcode = data.get("errcode", -1) errmsg = data.get("errmsg", "") detail = MP_ERROR_CODES.get(errcode, errmsg or f"未知错误(err_code={errcode})") return errcode, detail def is_token_expired(errcode: int) -> bool: return errcode in (40014, 42001)