‹ 首页

error-handling

@developersglobal · 收录于 1 周前

Graceful degradation and meaningful error messages. Errors are first-class citizens, not afterthoughts. Every error path is designed, not discovered.

适合你,如果想让程序在出错时依然表现优雅

/ 下载安装
error-handling.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add developersglobal/ai-agent-skills/error-handling
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- developersglobal/ai-agent-skills/error-handling
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify developersglobal/ai-agent-skills/error-handling
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
64GitHub stars
~766上下文体积 · 单文件
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · MIT · f47f948
Overview

Error handling is not defensive programming — it's a user experience. When things go wrong (and they will), the system should degrade gracefully, give users actionable information, and leave enough telemetry to diagnose and fix the problem.

When to Use
  • When writing any code that can fail (I/O, network, parsing, user input)
  • When reviewing error handling in existing code
  • Before any service goes to production
Process
Step 1: Design Error Paths Explicitly
  1. For every operation, list: what can fail? What does failure look like?
  2. Classify failures:
  3. Transient: Retry likely to succeed (network blip, temporary unavailability)
  4. Client error: Bad input from the caller (4xx) — don't retry
  5. System error: Internal failure (5xx) — alert, investigate
  6. Design the failure path for each class before writing the happy path.

Verify: Error classes defined for every external operation.

Step 2: Meaningful Error Messages
  1. Every error message answers: what went wrong? How can the caller fix it?
  2. ✅ "Invalid email format. Expected: user@domain.com"
  3. ❌ "Validation error"
  4. User-facing errors: friendly language, no stack traces.
  5. Developer-facing errors (logs): full context, request ID, stack trace.
  6. Never expose internal system details (DB schema, file paths) in user-facing errors.

Verify: Each error message would help a user or developer understand and fix the problem.

Step 3: Retry with Backoff
  1. Transient errors: retry with exponential backoff + jitter.
  2. Maximum retries: 3 (not infinite).
  3. After max retries: fail with a clear error, log the final failure.
  4. Non-transient errors (validation, auth): never retry.

Verify: Retry logic has a maximum. Non-transient errors don't retry.

Step 4: Graceful Degradation
  1. Identify non-critical dependencies. If they fail, degrade — don't crash.
  2. Example: recommendation engine fails → show default content, not 500.
  3. Circuit breaker pattern for failing dependencies: fail fast after threshold, recover automatically.

Verify: Every non-critical dependency has a defined degraded state.

Step 5: Structured Error Responses (APIs)
  1. API errors return consistent structure: ```json { "error": { "code": "INVALID_EMAIL", "message": "The email address format is invalid.", "requestId": "req_abc123" } } ```
  2. HTTP status codes used correctly: 400 (client error), 404 (not found), 429 (rate limited), 500 (server error).
Common Rationalizations (and Rebuttals)

| Excuse | Rebuttal | |--------|----------| | "I'll add error handling later" | Later means in production, under pressure, while users are impacted. | | "This can't fail" | Everything can fail. Network calls, disk writes, parsing — all can fail. | | "The error message doesn't matter" | It matters when a developer is debugging at 2am. |

Verification
  • [ ] Error classes defined for every external operation
  • [ ] Error messages answer: what went wrong? How to fix?
  • [ ] Transient errors retry with max limit
  • [ ] Non-critical dependencies have graceful degraded states
  • [ ] API errors return consistent structured format
  • [ ] No internal system details in user-facing errors
References
  • [observability skill](../observability/SKILL.md)
  • [debugging-methodology skill](../debugging-methodology/SKILL.md)
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

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