api-error-handling by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill api-error-handling通过标准化响应和适当的日志记录实现稳健的错误处理。
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid request parameters",
"status": 400,
"requestId": "req_abc123",
"timestamp": "2025-01-15T10:30:00Z",
"details": [
{ "field": "email", "message": "Invalid email format" }
]
}
}
class ApiError extends Error {
constructor(code, message, status = 500, details = null) {
super(message);
this.code = code;
this.status = status;
this.details = details;
}
static badRequest(message, details) {
return new ApiError('BAD_REQUEST', message, 400, details);
}
static notFound(resource) {
return new ApiError('NOT_FOUND', `${resource} not found`, 404);
}
static unauthorized() {
return new ApiError('UNAUTHORIZED', 'Authentication required', 401);
}
}
// 全局错误处理器
app.use((err, req, res, next) => {
const status = err.status || 500;
const response = {
error: {
code: err.code || 'INTERNAL_ERROR',
message: status === 500 ? 'Internal server error' : err.message,
status,
requestId: req.id
}
};
if (err.details) response.error.details = err.details;
if (status >= 500) logger.error(err);
res.status(status).json(response);
});
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
class CircuitBreaker {
constructor(threshold = 5, timeout = 30000) {
this.failures = 0;
this.threshold = threshold;
this.timeout = timeout;
this.state = 'CLOSED';
}
async call(fn) {
if (this.state === 'OPEN') throw new Error('Circuit open');
try {
const result = await fn();
this.failures = 0;
return result;
} catch (err) {
this.failures++;
if (this.failures >= this.threshold) {
this.state = 'OPEN';
setTimeout(() => this.state = 'HALF_OPEN', this.timeout);
}
throw err;
}
}
}
查看 references/python-flask.md 了解:
每周安装数
73
仓库
GitHub 星标数
90
首次出现
2026年1月22日
安全审计
已安装于
claude-code62
gemini-cli58
codex58
opencode57
cursor55
github-copilot53
Implement robust error handling with standardized responses and proper logging.
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid request parameters",
"status": 400,
"requestId": "req_abc123",
"timestamp": "2025-01-15T10:30:00Z",
"details": [
{ "field": "email", "message": "Invalid email format" }
]
}
}
class ApiError extends Error {
constructor(code, message, status = 500, details = null) {
super(message);
this.code = code;
this.status = status;
this.details = details;
}
static badRequest(message, details) {
return new ApiError('BAD_REQUEST', message, 400, details);
}
static notFound(resource) {
return new ApiError('NOT_FOUND', `${resource} not found`, 404);
}
static unauthorized() {
return new ApiError('UNAUTHORIZED', 'Authentication required', 401);
}
}
// Global error handler
app.use((err, req, res, next) => {
const status = err.status || 500;
const response = {
error: {
code: err.code || 'INTERNAL_ERROR',
message: status === 500 ? 'Internal server error' : err.message,
status,
requestId: req.id
}
};
if (err.details) response.error.details = err.details;
if (status >= 500) logger.error(err);
res.status(status).json(response);
});
class CircuitBreaker {
constructor(threshold = 5, timeout = 30000) {
this.failures = 0;
this.threshold = threshold;
this.timeout = timeout;
this.state = 'CLOSED';
}
async call(fn) {
if (this.state === 'OPEN') throw new Error('Circuit open');
try {
const result = await fn();
this.failures = 0;
return result;
} catch (err) {
this.failures++;
if (this.failures >= this.threshold) {
this.state = 'OPEN';
setTimeout(() => this.state = 'HALF_OPEN', this.timeout);
}
throw err;
}
}
}
See references/python-flask.md for:
Weekly Installs
73
Repository
GitHub Stars
90
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
claude-code62
gemini-cli58
codex58
opencode57
cursor55
github-copilot53
lark-cli 共享规则:飞书资源操作指南与权限配置详解
39,000 周安装
后端架构模式指南:整洁架构、六边形架构与领域驱动设计实践
123 周安装
GitHub PR 自动创建工具 - 支持任务验证、测试执行与 Conventional Commits 规范
121 周安装
Anthropic Claude API 开发指南:Messages API、工具使用与提示工程实战
122 周安装
演讲幻灯片内容创作指南:掌握陈述式、提问式标题与极简设计原则
121 周安装
响应式设计完整指南:移动优先、Flexbox、CSS Grid与性能优化实践
124 周安装
ToolUniverse 技能自动安装脚本 - 一键安装50+专业研究工具,支持Cursor、Claude等AI助手
122 周安装