ForcePilot/backend/package/yuxi/channel/extensions/slack/errors.py
Kris bfc7755137 feat(channel): 添加 Slack 渠道扩展
新增 Slack 渠道扩展,支持在 Yuxi 平台中集成 Slack 团队协作平台。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- monitor: 渠道状态监控
- status: 会话状态管理
- actions: 交互动作处理
- interactive: 交互式消息
- commands: 斜杠指令
- threading: 线程管理
- mentions: @提及
- constants: 常量定义
- types: 类型定义
2026-05-21 11:43:23 +08:00

100 lines
3.2 KiB
Python

from enum import StrEnum
class SlackErrorCode(StrEnum):
AUTH_FAILED = "auth_failed"
INVALID_AUTH = "invalid_auth"
ACCOUNT_INACTIVE = "account_inactive"
TOKEN_REVOKED = "token_revoked"
MISSING_SCOPE = "missing_scope"
RATE_LIMITED = "rate_limited"
CHANNEL_NOT_FOUND = "channel_not_found"
USER_NOT_FOUND = "user_not_found"
THREAD_NOT_FOUND = "thread_not_found"
NOT_IN_CHANNEL = "not_in_channel"
IS_ARCHIVED = "is_archived"
MSG_TOO_LONG = "msg_too_long"
RESTRICTED_ACTION = "restricted_action"
NETWORK_ERROR = "network_error"
CONNECTION_CLOSED = "connection_closed"
CONFIG_ERROR = "config_error"
TOKEN_EXPIRED = "token_expired"
MEDIA_UPLOAD_FAILED = "media_upload_failed"
STREAMING_FAILED = "streaming_failed"
SEND_FAILED = "send_failed"
ECHO_DETECTED = "echo_detected"
UNKNOWN = "unknown"
class SlackError(Exception):
def __init__(self, code: SlackErrorCode, message: str, retryable: bool = False):
self.code = code
self.retryable = retryable
super().__init__(f"[{code.value}] {message}")
class SlackRateLimitError(SlackError):
def __init__(self, message: str, retry_after: float = 0):
super().__init__(SlackErrorCode.RATE_LIMITED, message, retryable=True)
self.retry_after = retry_after
class SlackAuthError(SlackError):
def __init__(self, message: str, code: SlackErrorCode = SlackErrorCode.AUTH_FAILED):
super().__init__(code, message, retryable=False)
class SlackStreamNotDeliveredError(SlackError):
def __init__(self, pending_text: str, error_code: str):
super().__init__(
SlackErrorCode.STREAMING_FAILED,
f"Stream stopped but text never delivered. Buffer: {pending_text[:200]}",
retryable=False,
)
self.pending_text = pending_text
self.error_code = error_code
_ERROR_RETRY_MAP: dict[str, tuple[bool, float | None]] = {
"ratelimited": (True, None),
"service_unavailable": (True, 1.0),
"internal_error": (True, 3.0),
"fatal_error": (True, 5.0),
"timeout": (True, 0.5),
"request_timeout": (True, 0.5),
"network_error": (True, 1.0),
"channel_not_found": (False, None),
"user_not_found": (False, None),
"thread_not_found": (False, None),
"not_in_channel": (False, None),
"is_archived": (False, None),
"msg_too_long": (False, None),
"token_revoked": (False, None),
"invalid_auth": (False, None),
"account_inactive": (False, None),
"missing_scope": (True, None),
"restricted_action": (False, None),
"no_permission": (False, None),
"not_authed": (False, None),
"invalid_arguments": (False, None),
"invalid_arg_name": (False, None),
"invalid_array_arg": (False, None),
"invalid_charset": (False, None),
"invalid_form_data": (False, None),
"invalid_post_type": (False, None),
"missing_post_type": (False, None),
"team_added_to_org": (False, None),
}
def classify_slack_error(error_code: str) -> tuple[bool, float | None]:
entry = _ERROR_RETRY_MAP.get(error_code)
if entry:
return entry
return (False, None)
def is_retryable_error(error_code: str) -> bool:
retryable, _ = classify_slack_error(error_code)
return retryable