api-error-handling by aj-geddes/useful-ai-prompts
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill api-error-handling构建健壮的错误处理系统,包含标准化的错误响应、详细的日志记录、错误分类和用户友好的错误信息。本技能涵盖了从抛出类型化错误到日志记录、监控以及面向客户端的响应格式化的完整生命周期。
最小化标准化错误响应格式:
{
"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" }
]
}
}
自定义错误类 (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();
}
}
// 用法
throw new ApiError("NOT_FOUND", "User not found", 404);
throw new ApiError("VALIDATION_ERROR", "Missing fields", 422, fieldErrors);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
references/ 目录下的详细实现:
requestId 和 traceId 以便于可观测性ERROR 级别;将 4xx 错误记录为 WARN 级别每周安装量
131
代码仓库
GitHub 星标数
116
首次出现
Jan 21, 2026
安全审计
安装于
opencode111
gemini-cli109
codex103
cursor97
claude-code97
github-copilot94
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.
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);
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Error Codes & Response Format | Complete ERROR_CODES map, response formatter, global middleware (Node.js + Python) |
| Retry Strategies & Circuit Breaker | Exponential backoff, jitter, circuit breaker pattern |
| Monitoring & Tracking | Sentry integration, error rate metrics, /metrics/errors endpoint |
| Validation Patterns | Input validation, schema guards, detecting bad responses before errors occur |
requestId and traceId in every error for observabilityERROR level; log 4xx at WARN levelWeekly Installs
131
Repository
GitHub Stars
116
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode111
gemini-cli109
codex103
cursor97
claude-code97
github-copilot94
多语言测试生成技能 - AI驱动单元测试生成工具,支持多种编程语言
7,700 周安装
GitHub Copilot CLI 快速入门教程 - 终端AI助手从入门到精通
7,700 周安装
对话音频生成工具:使用Dia TTS创建逼真多说话人对话,支持情感控制和节奏调整
7,700 周安装
内容再利用工具:AI 一键将博客文章转换为社交媒体内容(Twitter、LinkedIn、短视频)
7,700 周安装
Transloadit 云端媒体处理 | 视频编码、图像优化、音频转码、OCR 文档
7,700 周安装
Power BI 数据模型设计审查指南 - 架构、性能与可维护性全面评估
7,700 周安装