error-handling-expert

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Error Handling Expert Skill

错误处理专家技能

Expert in error handling patterns, exception management, error responses, logging, and error recovery strategies for React, Next.js, and NestJS applications.
精通React、Next.js和NestJS应用中的错误处理模式、异常管理、错误响应、日志记录以及错误恢复策略。

When to Use

适用场景

  • Implementing error handling
  • Creating exception filters
  • Designing error responses
  • Setting up error logging
  • Implementing error recovery
  • Handling async errors
  • Creating error boundaries
  • Implementing retry logic
  • 实现错误处理
  • 创建异常过滤器
  • 设计错误响应
  • 设置错误日志
  • 实现错误恢复
  • 处理异步错误
  • 创建错误边界
  • 实现重试逻辑

Project Context Discovery

项目上下文探查

Before providing guidance:
  1. Check
    .agents/SYSTEM/ARCHITECTURE.md
    for error patterns
  2. Review existing exception filters
  3. Check for error monitoring (Sentry, Rollbar)
  4. Review logging libraries (Winston, Pino)
在提供指导之前:
  1. 查看
    .agents/SYSTEM/ARCHITECTURE.md
    中的错误模式
  2. 审查现有异常过滤器
  3. 检查错误监控工具(Sentry、Rollbar)
  4. 审查日志库(Winston、Pino)

Core Principles

核心原则

Error Types

错误类型

Application Errors: 400, 401, 403, 404, 409, 422 System Errors: 500, 502, 503, 504
应用错误: 400、401、403、404、409、422 系统错误: 500、502、503、504

Error Response Format

错误响应格式

typescript
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Validation failed",
    "details": [...],
    "timestamp": "2025-01-01T00:00:00Z",
    "path": "/api/users",
    "requestId": "req-123456"
  }
}
typescript
{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Validation failed",
    "details": [...],
    "timestamp": "2025-01-01T00:00:00Z",
    "path": "/api/users",
    "requestId": "req-123456"
  }
}

Quick Patterns

快速模式

NestJS Exception Filter

NestJS 异常过滤器

typescript
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    // Log, format, respond
  }
}
typescript
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    // Log, format, respond
  }
}

React Error Boundary

React 错误边界

typescript
class ErrorBoundary extends React.Component {
  componentDidCatch(error, errorInfo) {
    // Log to monitoring
  }
}
typescript
class ErrorBoundary extends React.Component {
  componentDidCatch(error, errorInfo) {
    // Log to monitoring
  }
}

Retry with Backoff

带退避的重试

typescript
async function retryWithBackoff<T>(fn, maxRetries = 3): Promise<T>
typescript
async function retryWithBackoff<T>(fn, maxRetries = 3): Promise<T>

Best Practices

最佳实践

  • User-friendly messages, no sensitive info
  • Log all errors with context
  • Integrate error monitoring (Sentry)
  • Implement retry logic and circuit breakers
  • Provide fallback values
  • Test error cases
  • 用户友好的提示信息,不包含敏感信息
  • 记录所有带上下文的错误
  • 集成错误监控(Sentry)
  • 实现重试逻辑和断路器
  • 提供回退值
  • 测试错误场景

Recovery Strategies

恢复策略

  1. Retry Logic - Exponential backoff
  2. Circuit Breaker - Prevent cascade failures
  3. Fallback Values - Graceful degradation

For complete exception filter implementations, custom exceptions, validation pipe setup, error boundaries, circuit breaker pattern, logging integration, and database/API error patterns, see:
references/full-guide.md
  1. 重试逻辑 - 指数退避
  2. 断路器 - 防止级联故障
  3. 回退值 - 优雅降级

如需完整的异常过滤器实现、自定义异常、验证管道设置、错误边界、断路器模式、日志集成以及数据库/API错误模式,请参阅:
references/full-guide.md