📄 types.ts • 3185 bytes
/**
* 容错系统 - 类型定义
* Phase 4: 错误分类 + 重试策略
*/
/** 错误严重程度 */
export type ErrorSeverity = 'low' | 'medium' | 'high' | 'critical'
/** 错误类别 */
export type ErrorCategory =
| 'network' // 网络错误
| 'timeout' // 超时错误
| 'syntax' // 语法错误
| 'type' // 类型错误
| 'runtime' // 运行时错误
| 'resource' // 资源不足
| 'permission' // 权限错误
| 'not_found' // 未找到
| 'validation' // 验证失败
| 'unknown' // 未知错误
/** 错误分类结果 */
export interface ErrorClassification {
category: ErrorCategory
severity: ErrorSeverity
message: string
code?: string
recoverable: boolean // 是否可恢复
suggestedAction: string // 建议操作
}
/** 重试策略类型 */
export type RetryStrategy =
| 'immediate' // 立即重试
| 'linear' // 线性等待
| 'exponential' // 指数退避
| 'fibonacci' // 斐波那契等待
/** 重试配置 */
export interface RetryConfig {
enabled: boolean
maxAttempts: number
initialDelay: number // 初始延迟(ms)
maxDelay: number // 最大延迟(ms)
strategy: RetryStrategy
backoffMultiplier: number // 退避乘数
jitter: boolean // 添加随机抖动
}
/** 默认重试配置 */
export const DEFAULT_RETRY_CONFIG: RetryConfig = {
enabled: true,
maxAttempts: 3,
initialDelay: 100,
maxDelay: 5000,
strategy: 'exponential',
backoffMultiplier: 2,
jitter: true,
}
/** 重试状态 */
export interface RetryState {
attempt: number
delay: number
nextRetryTime?: number
lastError?: string
history: RetryHistoryItem[]
}
/** 重试历史项 */
export interface RetryHistoryItem {
attempt: number
timestamp: number
error: string
duration: number
}
/** 执行结果 */
export interface ExecutionResult<T = any> {
success: boolean
data?: T
error?: ErrorClassification
attempts: number
duration: number
retries: RetryHistoryItem[]
}
/** 降级策略 */
export interface FallbackStrategy<T = any> {
name: string
handler: () => Promise<T>
condition?: (error: Error) => boolean
}
/** 错误分类器配置 */
export interface ClassifierConfig {
enableAI: boolean // 使用 AI 辅助分类
cacheClassifications: boolean
maxCacheSize: number
}
/** 错误类别标签 */
export const CATEGORY_LABELS: Record<ErrorCategory, string> = {
network: '🌐 网络错误',
timeout: '⏱️ 超时错误',
syntax: '📝 语法错误',
type: '🔤 类型错误',
runtime: '⚡ 运行时错误',
resource: '💾 资源不足',
permission: '🔐 权限错误',
not_found: '❓ 未找到',
validation: '✅ 验证失败',
unknown: '❓ 未知错误',
}
/** 严重程度标签 */
export const SEVERITY_LABELS: Record<ErrorSeverity, string> = {
low: '🟢 低',
medium: '🟡 中',
high: '🟠 高',
critical: '🔴 严重',
}
/** 重试策略标签 */
export const STRATEGY_LABELS: Record<RetryStrategy, string> = {
immediate: '⚡ 立即',
linear: '📈 线性',
exponential: '🚀 指数',
fibonacci: '🔢 斐波那契',
}