Communitygithub.com

actionbook/m13-domain-error

用於設計領域錯誤處理。關鍵字:領域錯誤、錯誤分類、恢復策略、重試、後備、領域錯誤層級、用戶面向與內部錯誤、錯誤碼設計、熔斷器、優雅降級。

m13-domain-error 是什麼?

此技巧旨在幫助開發者和AI代理設計穩健的領域錯誤處理機制。它涵蓋了錯誤分類(如暫時性 vs. 永久性錯誤)、恢復策略(包括重試、後備和帶退避的重試)、以及錯誤碼的設計原則。重點在於區分用戶面向和內部錯誤,並建立一個清晰的領域錯誤層級結構。此外,還探討了熔斷器模式和優雅降級等進階概念,以提升系統韌性。它適用於Claude Code、Codex等AI代理平台,並提供具體的實作指引和最佳實踐。通過此技巧,團隊可以標準化錯誤處理流程,減少系統故障時的混亂,並提高可維護性。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/actionbook/rust-skills/tree/main/skills/m13-domain-error

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

m13-domain-error 是做什麼的?

用於設計領域錯誤處理。關鍵字:領域錯誤、錯誤分類、恢復策略、重試、後備、領域錯誤層級、用戶面向與內部錯誤、錯誤碼設計、熔斷器、優雅降級、韌性、錯誤上下文、退避、帶退避的重試、錯誤恢復、暫時性 vs. 永久性錯誤。

此技巧幫助你在專案中建立一致的錯誤處理策略。透過分類和分層,你可以區分不同嚴重程度的錯誤,並針對暫時性錯誤實作重試邏輯,對永久性錯誤則觸發後備或優雅降級。設計錯誤碼時,應考慮可讀性和可除錯性,並在用戶面向的訊息中隱藏敏感細節。結合熔斷器模式,可以防止系統雪崩效應,確保整體服務的穩定性。

使用場景

  • 設計REST API或gRPC服務的錯誤回應格式
  • 定義微服務間的錯誤傳遞協議
  • 建立前端友好的錯誤顯示和處理規則
  • 優化後端錯誤日誌和監控

實作要點

# 暫時性錯誤範例
class TransientError(Exception):
    pass

# 永久性錯誤範例
class PermanentError(Exception):
    pass

相關資源:請參閱原始文件的錯誤層級圖和重試策略表格。

Individual skills in this repo

This repo contains 13 individual skills — each has its own dedicated page.

actionbook/m01-ownership

CRITICAL: Use for ownership/borrow/lifetime issues. Triggers: E0382, E0597, E0506, E0507, E0515, E0716, E0106, value moved, borrowed value does not live long enough, cannot move out of, use of moved value, ownership, borrow, lifetime, 'a, 'static, move, clone, Copy, 所有权, 借用, 生命周期

actionbook/m02-resource

CRITICAL: Use for smart pointers and resource management. Triggers: Box, Rc, Arc, Weak, RefCell, Cell, smart pointer, heap allocation, reference counting, RAII, Drop, should I use Box or Rc, when to use Arc vs Rc, 智能指针, 引用计数, 堆分配

actionbook/m03-mutability

CRITICAL: Use for mutability issues. Triggers: E0596, E0499, E0502, cannot borrow as mutable, already borrowed as immutable, mut, &mut, interior mutability, Cell, RefCell, Mutex, RwLock, 可变性, 内部可变性, 借用冲突

actionbook/m04-zero-cost

CRITICAL: Use for generics, traits, zero-cost abstraction. Triggers: E0277, E0308, E0599, generic, trait, impl, dyn, where, monomorphization, static dispatch, dynamic dispatch, impl Trait, trait bound not satisfied, 泛型, 特征, 零成本抽象, 单态化

actionbook/m05-type-driven

CRITICAL: Use for type-driven design. Triggers: type state, PhantomData, newtype, marker trait, builder pattern, make invalid states unrepresentable, compile-time validation, sealed trait, ZST, 类型状态, 新类型模式, 类型驱动设计

actionbook/m06-error-handling

CRITICAL: Use for error handling. Triggers: Result, Option, Error, ?, unwrap, expect, panic, anyhow, thiserror, when to panic vs return Result, custom error, error propagation, 错误处理, Result 用法, 什么时候用 panic

actionbook/m07-concurrency

CRITICAL: Use for concurrency/async. Triggers: E0277 Send Sync, cannot be sent between threads, thread, spawn, channel, mpsc, Mutex, RwLock, Atomic, async, await, Future, tokio, deadlock, race condition, 并发, 线程, 异步, 死锁

actionbook/m09-domain

CRITICAL: Use for domain modeling. Triggers: domain model, DDD, domain-driven design, entity, value object, aggregate, repository pattern, business rules, validation, invariant, 领域模型, 领域驱动设计, 业务规则

actionbook/m10-performance

CRITICAL: Use for performance optimization. Triggers: performance, optimization, benchmark, profiling, flamegraph, criterion, slow, fast, allocation, cache, SIMD, make it faster, 性能优化, 基准测试

actionbook/m11-ecosystem

Use when integrating crates or ecosystem questions. Keywords: E0425, E0433, E0603, crate, cargo, dependency, feature flag, workspace, which crate to use, using external C libraries, creating Python extensions, PyO3, wasm, WebAssembly, bindgen, cbindgen, napi-rs, cannot find, private, crate recommendation, best crate for, Cargo.toml, features, crate 推荐, 依赖管理, 特性标志, 工作空间, Python 绑定

actionbook/m12-lifecycle

Use when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup patterns, cleanup, scope, OnceCell, Lazy, once_cell, OnceLock, transaction, session management, when is Drop called, cleanup on error, guard pattern, scope guard, 资源生命周期, 连接池, 惰性初始化, 资源清理, RAII 模式

actionbook/m14-mental-model

Use when learning Rust concepts. Keywords: mental model, how to think about ownership, understanding borrow checker, visualizing memory layout, analogy, misconception, explaining ownership, why does Rust, help me understand, confused about, learning Rust, explain like I'm, ELI5, intuition for, coming from Java, coming from Python, 心智模型, 如何理解所有权, 学习 Rust, Rust 入门, 为什么 Rust

actionbook/unsafe-checker

CRITICAL: Use for unsafe Rust code review and FFI. Triggers on: unsafe, raw pointer, FFI, extern, transmute, *mut, *const, union, #[repr(C)], libc, std::ffi, MaybeUninit, NonNull, SAFETY comment, soundness, undefined behavior, UB, safe wrapper, memory layout, bindgen, cbindgen, CString, CStr, 安全抽象, 裸指针, 外部函数接口, 内存布局, 不安全代码, FFI 绑定, 未定义行为

相關技能