m13-domain-error
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDomain Error Strategy
领域错误策略
Layer 2: Design Choices
第二层:设计选择
Core Question
核心问题
Who needs to handle this error, and how should they recover?
Before designing error types:
- Is this user-facing or internal?
- Is recovery possible?
- What context is needed for debugging?
谁需要处理这个错误,以及他们应该如何恢复?
在设计错误类型之前:
- 这是面向用户的还是内部的?
- 是否可以恢复?
- 调试需要哪些上下文信息?
Error Categorization
错误分类
| Error Type | Audience | Recovery | Example |
|---|---|---|---|
| User-facing | End users | Guide action | |
| Internal | Developers | Debug info | |
| System | Ops/SRE | Monitor/alert | |
| Transient | Automation | Retry | |
| Permanent | Human | Investigate | |
| 错误类型 | 受众 | 恢复方式 | 示例 |
|---|---|---|---|
| 面向用户 | 终端用户 | 指导操作 | |
| 内部错误 | 开发人员 | 调试信息 | |
| 系统错误 | 运维/SRE | 监控/告警 | |
| 临时错误 | 自动化程序 | 重试 | |
| 永久错误 | 人工 | 调查处理 | |
Thinking Prompt
思考提示
Before designing error types:
-
Who sees this error?
- End user → friendly message, actionable
- Developer → detailed, debuggable
- Ops → structured, alertable
-
Can we recover?
- Transient → retry with backoff
- Degradable → fallback value
- Permanent → fail fast, alert
-
What context is needed?
- Call chain → anyhow::Context
- Request ID → structured logging
- Input data → error payload
在设计错误类型之前:
-
谁会看到这个错误?
- 终端用户 → 友好提示,可执行操作
- 开发人员 → 详细信息,便于调试
- 运维人员 → 结构化,可告警
-
是否可以恢复?
- 临时错误 → 带退避的重试
- 可降级 → 回退值
- 永久错误 → 快速失败,告警
-
需要哪些上下文信息?
- 调用链 → anyhow::Context
- 请求ID → 结构化日志
- 输入数据 → 错误载荷
Trace Up ↑
向上追溯 ↑
To domain constraints (Layer 3):
"How should I handle payment failures?"
↑ Ask: What are the business rules for retries?
↑ Check: domain-fintech (transaction requirements)
↑ Check: SLA (availability requirements)| Question | Trace To | Ask |
|---|---|---|
| Retry policy | domain-* | What's acceptable latency for retry? |
| User experience | domain-* | What message should users see? |
| Compliance | domain-* | What must be logged for audit? |
到领域约束(第三层):
"我应该如何处理支付失败?"
↑ 询问:重试的业务规则是什么?
↑ 查看:domain-fintech(交易要求)
↑ 查看:SLA(可用性要求)| 问题 | 追溯至 | 询问内容 |
|---|---|---|
| 重试策略 | domain-* | 重试可接受的延迟是多少? |
| 用户体验 | domain-* | 用户应该看到什么提示信息? |
| 合规性 | domain-* | 审计需要记录哪些内容? |
Trace Down ↓
向下追溯 ↓
To implementation (Layer 1):
"Need typed errors"
↓ m06-error-handling: thiserror for library
↓ m04-zero-cost: Error enum design
"Need error context"
↓ m06-error-handling: anyhow::Context
↓ Logging: tracing with fields
"Need retry logic"
↓ m07-concurrency: async retry patterns
↓ Crates: tokio-retry, backoff到实现层(第一层):
"需要类型化错误"
↓ m06-error-handling: 使用thiserror库
↓ m04-zero-cost: 错误枚举设计
"需要错误上下文"
↓ m06-error-handling: anyhow::Context
↓ 日志:带字段的tracing
"需要重试逻辑"
↓ m07-concurrency: 异步重试模式
↓ 依赖库:tokio-retry, backoffQuick Reference
快速参考
| Recovery Pattern | When | Implementation |
|---|---|---|
| Retry | Transient failures | exponential backoff |
| Fallback | Degraded mode | cached/default value |
| Circuit Breaker | Cascading failures | failsafe-rs |
| Timeout | Slow operations | |
| Bulkhead | Isolation | separate thread pools |
| 恢复模式 | 适用场景 | 实现方式 |
|---|---|---|
| 重试 | 临时故障 | 指数退避 |
| 回退 | 降级模式 | 缓存/默认值 |
| 熔断器 | 级联故障 | failsafe-rs |
| 超时 | 慢操作 | |
| 舱壁 | 隔离 | 独立线程池 |
Error Hierarchy
错误层级
rust
#[derive(thiserror::Error, Debug)]
pub enum AppError {
// User-facing
#[error("Invalid input: {0}")]
Validation(String),
// Transient (retryable)
#[error("Service temporarily unavailable")]
ServiceUnavailable(#[source] reqwest::Error),
// Internal (log details, show generic)
#[error("Internal error")]
Internal(#[source] anyhow::Error),
}
impl AppError {
pub fn is_retryable(&self) -> bool {
matches!(self, Self::ServiceUnavailable(_))
}
}rust
#[derive(thiserror::Error, Debug)]
pub enum AppError {
// User-facing
#[error("Invalid input: {0}")]
Validation(String),
// Transient (retryable)
#[error("Service temporarily unavailable")]
ServiceUnavailable(#[source] reqwest::Error),
// Internal (log details, show generic)
#[error("Internal error")]
Internal(#[source] anyhow::Error),
}
impl AppError {
pub fn is_retryable(&self) -> bool {
matches!(self, Self::ServiceUnavailable(_))
}
}Retry Pattern
重试模式
rust
use tokio_retry::{Retry, strategy::ExponentialBackoff};
async fn with_retry<F, T, E>(f: F) -> Result<T, E>
where
F: Fn() -> impl Future<Output = Result<T, E>>,
E: std::fmt::Debug,
{
let strategy = ExponentialBackoff::from_millis(100)
.max_delay(Duration::from_secs(10))
.take(5);
Retry::spawn(strategy, || f()).await
}rust
use tokio_retry::{Retry, strategy::ExponentialBackoff};
async fn with_retry<F, T, E>(f: F) -> Result<T, E>
where
F: Fn() -> impl Future<Output = Result<T, E>>,
E: std::fmt::Debug,
{
let strategy = ExponentialBackoff::from_millis(100)
.max_delay(Duration::from_secs(10))
.take(5);
Retry::spawn(strategy, || f()).await
}Common Mistakes
常见错误
| Mistake | Why Wrong | Better |
|---|---|---|
| Same error for all | No actionability | Categorize by audience |
| Retry everything | Wasted resources | Only transient errors |
| Infinite retry | DoS self | Max attempts + backoff |
| Expose internal errors | Security risk | User-friendly messages |
| No context | Hard to debug | .context() everywhere |
| 错误做法 | 问题所在 | 优化方案 |
|---|---|---|
| 所有场景使用相同错误 | 无法执行针对性操作 | 按受众分类 |
| 对所有错误重试 | 浪费资源 | 仅对临时错误重试 |
| 无限重试 | 自我拒绝服务 | 最大重试次数 + 退避 |
| 暴露内部错误 | 安全风险 | 面向用户的友好提示 |
| 无上下文信息 | 难以调试 | 处处使用.context() |
Anti-Patterns
反模式
| Anti-Pattern | Why Bad | Better |
|---|---|---|
| String errors | No structure | thiserror types |
| panic! for recoverable | Bad UX | Result with context |
| Ignore errors | Silent failures | Log or propagate |
| Box<dyn Error> everywhere | Lost type info | thiserror |
| Error in happy path | Performance | Early validation |
| 反模式 | 问题所在 | 优化方案 |
|---|---|---|
| 字符串错误 | 无结构 | thiserror类型 |
| 可恢复错误使用panic! | 用户体验差 | 带上下文的Result |
| 忽略错误 | 静默故障 | 记录或传播错误 |
| 处处使用Box<dyn Error> | 丢失类型信息 | thiserror |
| 正常流程中处理错误 | 性能问题 | 提前验证 |
Related Skills
相关技能
| When | See |
|---|---|
| Error handling basics | m06-error-handling |
| Retry implementation | m07-concurrency |
| Domain modeling | m09-domain |
| User-facing APIs | domain-* |
| 适用场景 | 参考内容 |
|---|---|
| 错误处理基础 | m06-error-handling |
| 重试实现 | m07-concurrency |
| 领域建模 | m09-domain |
| 面向用户的API | domain-* |