定义前端和后端的统一错误处理:
sequenceDiagram
participant Client as 客户端
participant API as API网关
participant Service as 业务服务
participant DB as 数据库
Client->>API: API请求
API->>Service: 调用业务逻辑
Service->>DB: 数据库操作
alt 操作成功
DB-->>Service: 成功结果
Service-->>API: 业务数据
API-->>Client: 成功响应
else 发生错误
DB-->>Service: 数据库错误
Service->>Service: 捕获并包装错误
Service-->>API: 标准错误格式
API->>API: 添加请求ID和时间戳
API-->>Client: 错误响应
Client->>Client: 统一错误处理
Client-->>User: 显示友好错误信息
end
interface ApiError {
error: {
code: string;
message: string;
details?: Record<string, any>;
timestamp: string;
requestId: string;
};
}