‹ 首页

api-error-handling

@aj-geddes · 收录于 5 天前 · 上游提交 4 个月前

Implement comprehensive API error handling with standardized error responses, logging, monitoring, retry logic, and validation patterns. Use when building resilient APIs, debugging issues, improving error reporting, implementing retry logic, handling HTTP error codes, managing API timeouts, designing error response handling, or adding circuit breaker patterns.

适合你,如果你在构建或维护需要高可靠性的 API 服务

/ 通过 npx 安装 校验哈希
npx oh-my-skill add aj-geddes/useful-ai-prompts/api-error-handling
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- aj-geddes/useful-ai-prompts/api-error-handling
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify aj-geddes/useful-ai-prompts/api-error-handling
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
300GitHub stars
~709最小装载
~4.1K含声明引用
~4.1K文本包总量
索引托管

怎么用

商店整理自技能原文 · 版本 3f5182c · 表述以原文为准
它做什么

当Claude编写API代码时,它会生成标准化的错误处理结构,包括统一的错误响应格式(含错误代码、状态码、请求ID等)、自定义错误类、日志级别建议、重试逻辑和断路器模式,以及输入验证代码。

什么时候触发

当你要求构建稳健的API、调试问题、改进错误报告、实现重试逻辑、处理HTTP错误码、管理超时、设计错误响应或添加断路器模式时触发。

装好后可以这样说
Claude会提供错误响应格式代码和全局错误处理中间件。
Claude会生成重试策略与断路器实现。
Claude会生成验证模式代码。
技能原文 SKILL.md作者撰写 · MIT · 3f5182c

API Error Handling

Table of Contents
  • [Overview](#overview)
  • [When to Use](#when-to-use)
  • [Quick Start](#quick-start)
  • [Reference Guides](#reference-guides)
  • [Best Practices](#best-practices)
Overview

Build robust error handling systems with standardized error responses, detailed logging, error categorization, and user-friendly error messages. This skill covers the full lifecycle from throwing typed errors through logging, monitoring, and client-facing response formatting.

When to Use
  • Handling API errors consistently across endpoints
  • Debugging production issues with request tracing
  • Implementing error recovery strategies (retry, circuit breaker)
  • Monitoring and alerting on error rates
  • Providing meaningful, actionable error messages to clients
  • Validating request inputs before processing
  • Tracking error patterns over time
Quick Start

Minimal standardized error response format:

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Input validation failed",
    "statusCode": 422,
    "requestId": "req_abc123xyz789",
    "timestamp": "2025-01-15T10:30:00Z",
    "details": [
      { "field": "email", "message": "Invalid email format", "code": "INVALID_EMAIL" }
    ]
  }
}

Custom error class (Node.js):

class ApiError extends Error {
  constructor(code, message, statusCode = null, details = null) {
    super(message);
    this.code = code;
    this.statusCode = statusCode || ERROR_CODES[code]?.status || 500;
    this.details = details;
    this.timestamp = new Date().toISOString();
  }
}

// Usage
throw new ApiError("NOT_FOUND", "User not found", 404);
throw new ApiError("VALIDATION_ERROR", "Missing fields", 422, fieldErrors);
Reference Guides

Detailed implementations in the references/ directory:

| Guide | Contents | |---|---| | [Error Codes & Response Format](references/error-codes-reference.md) | Complete ERROR_CODES map, response formatter, global middleware (Node.js + Python) | | [Retry Strategies & Circuit Breaker](references/retry-strategies.md) | Exponential backoff, jitter, circuit breaker pattern | | [Monitoring & Tracking](references/monitoring-patterns.md) | Sentry integration, error rate metrics, /metrics/errors endpoint | | [Validation Patterns](references/validation-examples.md) | Input validation, schema guards, detecting bad responses before errors occur |

Best Practices
✅ DO
  • Use a consistent error response format across all endpoints
  • Include requestId and traceId in every error for observability
  • Log 5xx errors at ERROR level; log 4xx at WARN level
  • Provide actionable error messages — tell the client what to fix
  • Use standard HTTP status codes (4xx client errors, 5xx server errors)
  • Implement retry with exponential backoff for transient failures
  • Use circuit breakers to prevent cascade failures
  • Validate inputs early and return all field errors at once
  • Monitor error rates and alert on anomalous spikes
❌ DON'T
  • Expose stack traces or internal implementation details to clients
  • Return HTTP 200 for error responses
  • Silently swallow errors
  • Log sensitive data (passwords, tokens, PII)
  • Use vague messages like "Something went wrong"
  • Mix error handling logic with business logic
  • Retry non-idempotent operations or client errors (4xx)
  • Return different error shapes from different endpoints
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。