stt-error.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. export type SttErrorCode =
  2. | 'NOT_INITIALIZED'
  3. | 'NOT_JOINED'
  4. | 'INVALID_CONFIG'
  5. | 'INVALID_LANGUAGES'
  6. | 'ALREADY_JOINED'
  7. | 'NETWORK_ERROR'
  8. | 'AUTHENTICATION_ERROR'
  9. | 'PERMISSION_DENIED'
  10. | 'TIMEOUT'
  11. | 'UNKNOWN_ERROR'
  12. | 'APP_ID_REQUIRED'
  13. export class SttError extends Error {
  14. public readonly code: SttErrorCode
  15. public readonly timestamp: number
  16. public readonly details?: Record<string, any>
  17. constructor(code: SttErrorCode, message: string, details?: Record<string, any>) {
  18. super(message)
  19. this.name = 'SttError'
  20. this.code = code
  21. this.timestamp = Date.now()
  22. this.details = details
  23. // 保持正确的原型链
  24. Object.setPrototypeOf(this, SttError.prototype)
  25. }
  26. static fromError(error: unknown, code: SttErrorCode = 'UNKNOWN_ERROR'): SttError {
  27. if (error instanceof SttError) {
  28. return error
  29. }
  30. if (error instanceof Error) {
  31. return new SttError(code, error.message, { originalError: error })
  32. }
  33. return new SttError(code, String(error))
  34. }
  35. toJSON(): Record<string, any> {
  36. return {
  37. name: this.name,
  38. code: this.code,
  39. message: this.message,
  40. timestamp: this.timestamp,
  41. details: this.details,
  42. stack: this.stack,
  43. }
  44. }
  45. toString(): string {
  46. return `[${this.code}] ${this.message}`
  47. }
  48. }
  49. // 错误处理工具函数
  50. export const createErrorHandler = (emitter?: { emit: (event: string, error: Error) => void }) => {
  51. return (error: unknown, context?: string): SttError => {
  52. const sttError = SttError.fromError(error)
  53. if (context) {
  54. Object.assign(sttError, {
  55. details: {
  56. ...sttError.details,
  57. context,
  58. },
  59. })
  60. }
  61. // 如果提供了事件发射器,发送错误事件
  62. if (emitter) {
  63. emitter.emit('error', sttError)
  64. }
  65. console.error(`[STT SDK Error] ${sttError.toString()}`, sttError.details)
  66. return sttError
  67. }
  68. }
  69. // 错误恢复策略
  70. export class ErrorRecovery {
  71. private maxRetries: number
  72. private retryDelay: number
  73. constructor(maxRetries = 3, retryDelay = 1000) {
  74. this.maxRetries = maxRetries
  75. this.retryDelay = retryDelay
  76. }
  77. async retry<T>(
  78. operation: () => Promise<T>,
  79. shouldRetry?: (error: SttError) => boolean
  80. ): Promise<T> {
  81. let lastError: SttError = new SttError('UNKNOWN_ERROR', 'Operation failed')
  82. for (let attempt = 1; attempt <= this.maxRetries; attempt++) {
  83. try {
  84. return await operation()
  85. } catch (error) {
  86. const sttError = SttError.fromError(error)
  87. lastError = sttError
  88. // 检查是否应该重试
  89. if (shouldRetry && !shouldRetry(sttError)) {
  90. break
  91. }
  92. // 检查是否达到最大重试次数
  93. if (attempt === this.maxRetries) {
  94. break
  95. }
  96. // 等待一段时间后重试
  97. await this.delay(this.retryDelay * attempt)
  98. }
  99. }
  100. throw lastError
  101. }
  102. private delay(ms: number): Promise<void> {
  103. return new Promise((resolve) => setTimeout(resolve, ms))
  104. }
  105. }