AgentSkillsCN

Global Error Handling

实施全面的错误处理机制,通过自定义错误类(AppError、ValidationError、AuthenticationError、AuthorizationError、NotFoundError、ConflictError、InternalServerError)以及标准化的API错误响应,配合完善的日志记录与优雅的UI错误状态,同时避免泄露敏感信息。在创建自定义错误类、实现错误中间件、处理API错误、记录错误日志、在React中创建错误边界、运用try-catch块、定义错误响应格式,或设置错误监控时,可使用此技能。适用于处理API路由处理器、中间件文件、服务层错误处理、React错误边界、错误日志配置,或任何可能抛出或捕获错误的代码。此技能确保自定义错误类继承AppError,以保障类型安全与结构一致性;采用标准化的错误响应格式,其中success: false,并附带error对象(message、code、details);根据日志级别(DEBUG/INFO/WARN/ERROR)进行记录,但绝不记录敏感数据(如密码、API密钥、PII);在Bun中使用Pino(最快的JSON日志库),在.NET中使用Serilog,Python中使用loguru,通过asyncHandler包装器集中管理错误中间件,为UI错误提供React错误边界,并配备人性化的回退机制;在TanStack Query中运用智能重试逻辑(不重试4xx错误),贯彻尽早验证的原则,同时可选集成Sentry以实现生产环境的监控。

SKILL.md
--- frontmatter
name: Global Error Handling
description: Implement comprehensive error handling with custom error classes (AppError, ValidationError, AuthenticationError, AuthorizationError, NotFoundError, ConflictError, InternalServerError), standardized API error responses, proper logging, and graceful UI error states without exposing sensitive information. Use this skill when creating custom error classes, implementing error middleware, handling API errors, logging errors, creating error boundaries in React, implementing try-catch blocks, defining error response formats, or setting up error monitoring. Apply when working on API route handlers, middleware files, service layer error handling, React error boundaries, error logging configuration, or any code that can throw or catch errors. This skill ensures custom error classes extending AppError for type safety and consistent structure, standardized error response format with success: false and error object (message, code, details), appropriate log levels (DEBUG/INFO/WARN/ERROR) without logging sensitive data (passwords, API keys, PII), Pino for Bun (fastest JSON logger), Serilog for .NET, loguru for Python, centralized error middleware with asyncHandler wrapper, React error boundaries for UI errors with user-friendly fallback, TanStack Query error handling with smart retry logic (don't retry 4xx), fail-early validation principles, and optional Sentry integration for production monitoring.

Global Error Handling

When to use this skill:

  • When creating custom error classes (ValidationError, AuthenticationError, AuthorizationError, NotFoundError, ConflictError)
  • When implementing API error handling middleware with asyncHandler wrapper
  • When defining error response formats for APIs ({success: false, error: {message, code, details}})
  • When writing try-catch blocks for async operations with specific error type handling
  • When implementing error logging (Pino for Bun, Serilog for .NET, loguru for Python)
  • When creating React error boundaries for UI error handling with fallback UI
  • When handling errors in API calls with TanStack Query (smart retry logic)
  • When configuring error monitoring services (Sentry for production)
  • When validating inputs and throwing errors early (fail-early principle)
  • When implementing retry logic for transient failures (not for 4xx client errors)
  • When working on error handling in service files, middleware, or API routes
  • When ensuring errors don't expose sensitive information to users
  • When logging to Google Cloud Logging in production environments
  • When wrapping Express/Bun routes with asyncHandler to catch promise rejections

This Skill provides Claude Code with specific guidance on how to adhere to coding standards as they relate to how it should handle global error handling.

Instructions

For details, refer to the information provided in this file: global error handling