Skill站外收录 @skilllib/skill-2117939930272413836
error-recovery
建立系统化错误处理体系,区分运行时异常与代码缺陷,提供输入校验、分级日志、渐进式重试、降级响应和补偿机制等能力,确保故障可追溯、反馈可操作、恢复可预期。
软件开发 AI 技能
Skills.MD
# Error Handling
## When to Use
- Implementing input validation at system boundaries
- Designing error responses for APIs or user interfaces
- Building recovery mechanisms for transient failures
- Establishing logging and monitoring patterns
- Distinguishing between errors that need user action vs system intervention
## Philosophy
Errors are not exceptional - they are expected. Good error handling treats errors as first-class citizens of the system design, not afterthoughts. The goal is to fail safely, provide actionable feedback, and enable recovery.
## Error Classification
### Operational Errors
Runtime problems that occur during normal operation. These are expected and must be handled.
**Characteristics:**
- External system failures (network, database, filesystem)
- Invalid user input
- Resource exhaustion (memory, disk, connections)
- Timeout conditions
- Rate limiting
**Response:** Handle gracefully, log appropriately, provide user feedback, implement recovery.
### Programmer Errors
Bugs in the code that should not happen if the code is correct.
**Characteristics:**
- Type errors caught at runtime
- Null/undefined access on required values
- Failed assertions on invariants
- Invalid internal state
**Response:** Fail fast, log full context, alert developers. Do not attempt recovery - fix the bug.
## Core Patterns
### Input Validation
Validate early, validate completely, provide specific feedback.
```
PATTERN: Fail-Fast Validation
1. Validate at system boundaries (API entry, user input, file reads)
2. Check all constraints before processing
3. Return ALL validation errors, not just the first one
4. Include field name, actual value (if safe), and expected format
5. Never trust data from external sources
```
**Validation checklist:**
- Required fields present
- Types correct
- Values within allowed ranges
- Formats match expectations (email, URL, date)
- Business rules satisfied
### Error Messages
Different audiences need different information.
**User-facing errors:**
- Clear action the user can take
- No technical jargon or stack traces
- Consistent tone and format
- Localization-ready
**Internal/logged errors:**
- Full technical context
- Request/correlation IDs
- Timestamp and service identifier
- Stack trace for programmer errors
- Sanitized sensitive data
### Recovery Strategies
**Retry with backoff:**
- Transient failures (network timeouts, rate limits)
- Exponential backoff with jitter
- Maximum retry count with circuit breaker
**Fallback:**
- Degraded functionality over complete failure
- Cached data when live data unavailable
- Default values when configuration missing
**Compensation:**
- Undo partial operations on failure
- Maintain consistency in distributed operations
- Saga pattern for multi-step processes
### Logging Levels
| Level | Use For |
|-------|---------|
| ERROR | Operational errors requiring attention |
| WARN | Recoverable issues, degraded performance |
| INFO | Significant state changes, request lifecycle |
| DEBUG | Detailed flow for troubleshooting |
**What to log:**
- Correlation/request ID
- User context (sanitized)
- Operation being attempted
- Error type and message
- Duration and timing
**What NOT to log:**
- Passwords, tokens, secrets
- Full credit card numbers
- Personal identifiable information (PII)
- Raw request/response bodies containing sensitive data
## Best Practices
- Fail fast on programmer errors - do not mask bugs
- Handle operational errors gracefully with recovery options
- Provide correlation IDs for tracing requests across services
- Use structured logging (JSON) for machine parseability
- Centralize error handling logic - avoid scattered try/catch blocks
- Test error paths as rigorously as success paths
- Monitor error rates and set alerts for anomalies
## Anti-Patterns
- Catching all exceptions silently (`catch {}`)
- Logging sensitive data in error messages
- Returning generic "Something went wrong" without context
- Retrying non-idempotent operations without safeguards
- Mixing validation errors with system errors in responses
- Treating all errors the same regardless of recoverability
## References
- [examples/error-patterns.md](examples/error-patterns.md) - Concrete examples across languages
评论 0
还没有评论,来抢沙发。
相关内容
同类或同场景的收录内容
building-with-kafka-strimzi
在 Kubernetes 上构建基于 Apache Kafka 的事件驱动系统,支持高可靠的消息传递、事务性处理与精确一次语义,结合 Strimzi 运营商实现集群自动化管理,利用 Schema Registry 保障事件模式演进,适用于微服务间解耦通信与实时数据流处理场景。
站外数据与分析
609 0.0
personal-tool-builder
专注于从个人真实痛点出发快速构建实用工具,涵盖命令行程序、本地优先应用和自动化脚本的开发与演进,强调先满足自身需求再逐步扩展,通过持续自用打磨功能,避免过度设计,确保工具简洁有效且长期可维护。
站外商业与营销
196 0.0
threejs-lighting
实现三维场景中的光照与阴影控制,支持多种光源类型及环境光照明,提供从基础布光到高级阴影优化的完整方案,适用于需要真实感渲染或性能平衡的可视化项目。
站外效率工具
442 0.0
aggregation-helper
提供数据聚合场景的自动化辅助能力,涵盖 SQL 查询编写、统计分析建模、可视化方案设计及商业智能配置,支持从需求理解到生产级代码生成的全流程指导。
站外效率工具数据与分析
217 0.0
举报「error-recovery」
举报信息仅审核团队可见,同一资产 24 小时内仅可举报一次