All files / utils errorHandler.ts

83.33% Statements 70/84
70.23% Branches 59/84
80% Functions 8/10
83.33% Lines 70/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288                                                      41x 41x 41x 41x 41x             12x                                                                       17x   2x   2x   2x   3x   1x   2x   3x   1x   1x                 34x 17x 3x   14x 1x         30x 17x 17x 17x     17x 17x 12x   12x   12x 4x 8x   8x                       17x 2x 15x 2x 13x 3x 10x 3x     17x       13x 1x 1x     1x       12x 7x       5x               10x   1x         1x       1x   1x             1x       5x               10x 10x     10x 10x               10x             3x   3x               3x     3x   3x                                           6x 6x                 6x 6x                        
/**
 * 统一错误处理工具
 * 处理 API 错误、网络错误、验证错误等
 */
 
import { ZodError } from 'zod';
 
/**
 * API 错误类型
 */
export interface ApiErrorResponse {
  status: number;
  message: string;
  detail?: string;
  error?: string;
  errors?: Record<string, string[]>;
}
 
/**
 * 标准化的错误对象
 */
export class AppError extends Error {
  public readonly code: string;
  public readonly status?: number;
  public readonly details?: any;
 
  constructor(message: string, code: string = 'UNKNOWN_ERROR', status?: number, details?: any) {
    super(message);
    this.name = 'AppError';
    this.code = code;
    this.status = status;
    this.details = details;
  }
}
 
/**
 * 错误代码常量
 */
export const ErrorCodes = {
  // 网络错误
  NETWORK_ERROR: 'NETWORK_ERROR',
  TIMEOUT_ERROR: 'TIMEOUT_ERROR',
 
  // 认证错误
  AUTH_REQUIRED: 'AUTH_REQUIRED',
  AUTH_EXPIRED: 'AUTH_EXPIRED',
  AUTH_INVALID: 'AUTH_INVALID',
 
  // 权限错误
  PERMISSION_DENIED: 'PERMISSION_DENIED',
 
  // 资源错误
  NOT_FOUND: 'NOT_FOUND',
  ALREADY_EXISTS: 'ALREADY_EXISTS',
 
  // 验证错误
  VALIDATION_ERROR: 'VALIDATION_ERROR',
  INVALID_INPUT: 'INVALID_INPUT',
 
  // 服务器错误
  SERVER_ERROR: 'SERVER_ERROR',
  SERVICE_UNAVAILABLE: 'SERVICE_UNAVAILABLE',
 
  // 业务错误
  BUSINESS_ERROR: 'BUSINESS_ERROR',
 
  // 未知错误
  UNKNOWN_ERROR: 'UNKNOWN_ERROR',
} as const;
 
/**
 * 从 HTTP 状态码获取错误代码
 */
function getErrorCodeFromStatus(status: number): string {
  switch (status) {
    case 400:
      return ErrorCodes.INVALID_INPUT;
    case 401:
      return ErrorCodes.AUTH_REQUIRED;
    case 403:
      return ErrorCodes.PERMISSION_DENIED;
    case 404:
      return ErrorCodes.NOT_FOUND;
    case 409:
      return ErrorCodes.ALREADY_EXISTS;
    case 422:
      return ErrorCodes.VALIDATION_ERROR;
    case 500:
      return ErrorCodes.SERVER_ERROR;
    case 503:
      return ErrorCodes.SERVICE_UNAVAILABLE;
    default:
      return ErrorCodes.UNKNOWN_ERROR;
  }
}
 
/**
 * 解析 API 错误响应
 */
export function parseApiError(error: any): AppError {
  // 网络错误
  if (!error.response && error.message) {
    if (error.message.includes('Network request failed')) {
      return new AppError('网络连接失败,请检查网络设置', ErrorCodes.NETWORK_ERROR);
    }
    if (error.message.includes('timeout')) {
      return new AppError('请求超时,请稍后重试', ErrorCodes.TIMEOUT_ERROR);
    }
  }
 
  // API 响应错误
  if (error.response || (error.status && typeof error.status === 'number')) {
    const status = error.response ? error.response.status : error.status;
    const data = error.response ? error.response.data : error;
    const errorCode = getErrorCodeFromStatus(status);
 
    // 提取错误消息
    let message = '请求失败';
    if (data) {
      Iif (typeof data === 'string') {
        message = data;
      } else Iif (data.detail) {
        message = data.detail;
      } else if (data.message) {
        message = data.message;
      } else Iif (data.error) {
        message = data.error;
      } else Iif (data.errors) {
        // 处理字段级错误
        const fieldErrors = Object.entries(data.errors)
          .map(
            ([field, errors]) => `${field}: ${Array.isArray(errors) ? errors.join(', ') : errors}`
          )
          .join('\n');
        message = fieldErrors || '输入数据有误';
      }
    }
 
    // 特殊处理一些常见错误
    if (status === 401) {
      message = '登录已过期,请重新登录';
    } else if (status === 403) {
      message = '没有权限执行此操作';
    } else if (status === 404) {
      message = '请求的资源不存在';
    } else if (status === 500) {
      message = '服务器错误,请稍后重试';
    }
 
    return new AppError(message, errorCode, status, data);
  }
 
  // Zod 验证错误
  if (error instanceof ZodError) {
    const firstError = error.errors[0];
    const message = firstError
      ? `${firstError.path.join('.')}: ${firstError.message}`
      : '数据验证失败';
    return new AppError(message, ErrorCodes.VALIDATION_ERROR, undefined, error.errors);
  }
 
  // 其他错误
  if (error instanceof Error) {
    return new AppError(error.message, ErrorCodes.UNKNOWN_ERROR);
  }
 
  // 未知错误
  return new AppError('发生未知错误', ErrorCodes.UNKNOWN_ERROR);
}
 
/**
 * 获取用户友好的错误消息
 */
export function getUserFriendlyMessage(error: AppError): string {
  // 根据错误代码返回友好的消息
  switch (error.code) {
    case ErrorCodes.NETWORK_ERROR:
      return '网络连接失败,请检查网络设置';
    case ErrorCodes.TIMEOUT_ERROR:
      return '请求超时,请稍后重试';
    case ErrorCodes.AUTH_REQUIRED:
    case ErrorCodes.AUTH_EXPIRED:
      return '登录已过期,请重新登录';
    case ErrorCodes.AUTH_INVALID:
      return '登录信息无效';
    case ErrorCodes.PERMISSION_DENIED:
      return '没有权限执行此操作';
    case ErrorCodes.NOT_FOUND:
      return '请求的资源不存在';
    case ErrorCodes.ALREADY_EXISTS:
      return '资源已存在';
    case ErrorCodes.VALIDATION_ERROR:
    case ErrorCodes.INVALID_INPUT:
      return error.message || '输入数据有误';
    case ErrorCodes.SERVER_ERROR:
      return '服务器错误,请稍后重试';
    case ErrorCodes.SERVICE_UNAVAILABLE:
      return '服务暂时不可用,请稍后重试';
    default:
      return error.message || '操作失败,请重试';
  }
}
 
/**
 * 处理错误并返回用户友好的消息
 */
export function handleError(error: any): string {
  const appError = parseApiError(error);
  const friendlyMessage = getUserFriendlyMessage(appError);
 
  // 在开发环境下打印详细错误
  Eif (__DEV__) {
    console.error('Error Details:', {
      code: appError.code,
      message: appError.message,
      status: appError.status,
      details: appError.details,
    });
  }
 
  return friendlyMessage;
}
 
/**
 * 错误日志记录
 */
export function logError(error: any, context?: string) {
  const appError = parseApiError(error);
 
  const errorLog = {
    code: appError.code,
    message: appError.message,
    status: appError.status,
    details: appError.details,
    timestamp: new Date().toISOString(),
  };
 
  console.error(`[Error${context ? ` - ${context}` : ''}]:`, errorLog);
 
  // 在生产环境中将错误发送到错误追踪服务
  if (__DEV__) {
    // 开发环境:仅控制台输出
    console.debug('[Dev] Full error details:', errorLog);
  } else E{
    // 生产环境:可以集成 Sentry 或其他错误追踪服务
    // 示例:
    // if (typeof Sentry !== 'undefined') {
    //   Sentry.captureException(appError, {
    //     extra: errorLog,
    //   });
    // }
 
    // 对于严重错误(5xx),可以考虑发送到后端日志系统
    if (appError.status && appError.status >= 500) {
      // 可以调用后端日志接口记录严重错误
      // logErrorToBackend(errorLog).catch(console.error);
    }
  }
}
 
/**
 * 检查是否是认证错误
 */
export function isAuthError(error: any): boolean {
  const appError = parseApiError(error);
  return (
    [ErrorCodes.AUTH_REQUIRED, ErrorCodes.AUTH_EXPIRED, ErrorCodes.AUTH_INVALID] as string[]
  ).includes(appError.code);
}
 
/**
 * 检查是否是网络错误
 */
export function isNetworkError(error: any): boolean {
  const appError = parseApiError(error);
  return ([ErrorCodes.NETWORK_ERROR, ErrorCodes.TIMEOUT_ERROR] as string[]).includes(appError.code);
}
 
/**
 * 检查是否是服务器错误
 */
export function isServerError(error: any): boolean {
  const appError = parseApiError(error);
  return ([ErrorCodes.SERVER_ERROR, ErrorCodes.SERVICE_UNAVAILABLE] as string[]).includes(
    appError.code
  );
}