ForcePilot/backend/package/yuxi/channel/extensions/msteams/errors.py
Kris 94444ced96 feat(channel): 添加 Microsoft Teams 渠道扩展
新增 Microsoft Teams 渠道扩展,支持在 Yuxi 平台中集成 Microsoft Teams 协作平台。

包含以下功能模块:
- sdk: Bot Framework SDK 封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- auth: JWT 认证
- jwks: JWKS 密钥管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- state: 状态管理
- runtime: 运行时管理
- actions: 动作处理
- adaptive_card: 自适应卡片
- task_modules: 任务模块
- message_extension: 消息扩展
- proactive: Proactive Messaging
- graph: Microsoft Graph API 集成
- graph_teams: Teams 操作
- graph_members: 成员管理
- graph_messages: 消息获取
- graph_thread: 线程管理
- graph_users: 用户管理
- graph_upload: 文件上传
- files: 文件处理
- file_consent: 文件授权
- conversations: 会话存储
- mentions: @提及处理
- threading: 线程管理
- reactions: 表情反应
- polls: 投票功能
- meetings: 会议集成
- feedback: 反馈处理
- sso: 单点登录
- deep_links: 深层链接
- incoming_webhook: 入站 Webhook
- localization: 本地化
- user_agent: 用户代理
- sent_message_cache: 消息缓存
- types: 类型定义
2026-05-21 11:28:42 +08:00

91 lines
2.5 KiB
Python

from __future__ import annotations
import logging
from .types import MSTeamsErrorCode
logger = logging.getLogger(__name__)
MAX_RETRIES = 3
MAX_RETRY_DELAY_MS = 10000
RETRY_BASE_MS = 1000
class MSTeamsError(Exception):
def __init__(self, error_code: MSTeamsErrorCode, message: str, status_code: int | None = None):
self.error_code = error_code
self.status_code = status_code
super().__init__(message)
class MSTeamsAuthError(MSTeamsError):
pass
class MSTeamsThrottledError(MSTeamsError):
pass
class MSTeamsTransientError(MSTeamsError):
pass
class MSTeamsPermanentError(MSTeamsError):
pass
class MSTeamsNetworkError(MSTeamsError):
pass
def classify_http_error(status_code: int, response_body: dict | None = None) -> MSTeamsErrorCode:
if status_code in (401, 403):
return MSTeamsErrorCode.AUTH
if status_code == 429:
return MSTeamsErrorCode.THROTTLED
if status_code in (500, 502, 503, 504):
return MSTeamsErrorCode.SERVICE_UNAVAILABLE
if status_code in (408,):
return MSTeamsErrorCode.TRANSIENT
if status_code == 400:
error_body = response_body or {}
error = error_body.get("error", {})
if isinstance(error, dict):
if error.get("code") == "BadArgument" and "replyToId" in str(error.get("message", "")):
return MSTeamsErrorCode.TRANSIENT
return MSTeamsErrorCode.BAD_REQUEST
if status_code == 404:
return MSTeamsErrorCode.NOT_FOUND
return MSTeamsErrorCode.PERMANENT
def is_retryable(error_code: MSTeamsErrorCode) -> bool:
return error_code in (MSTeamsErrorCode.THROTTLED, MSTeamsErrorCode.TRANSIENT)
def retry_delay_ms(attempt: int) -> int:
return min(RETRY_BASE_MS * (2**attempt), MAX_RETRY_DELAY_MS)
def classify_exception(exc: Exception) -> MSTeamsErrorCode:
import httpx
if isinstance(exc, MSTeamsError):
return exc.error_code
if isinstance(exc, httpx.HTTPStatusError):
return classify_http_error(exc.response.status_code)
if isinstance(exc, httpx.NetworkError) or isinstance(exc, (ConnectionError, TimeoutError)):
return MSTeamsErrorCode.NETWORK
return MSTeamsErrorCode.PERMANENT
def build_error_for_response(status_code: int, body: dict | None = None) -> MSTeamsError:
error_code = classify_http_error(status_code, body)
msg = f"HTTP {status_code}"
if body:
msg += f": {str(body)[:200]}"
return MSTeamsError(error_code, msg, status_code)