Back to Skills

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

1,376stars110forksUpdated 8/6/2026

Security Assessment

Safe(96/100)
Security Score96/100

About m06-error-handling

This skill is a decision-oriented reference for error handling in Rust, part of the actionbook "rust-skills" module set (module m06, "Layer 1: Language Mechanics"). It exists to stop developers from reflexively reaching for `unwrap()` or `Box<dyn Error>` and instead reframes each error as a design question: is this failure expected or a bug, who should handle it, and what context does the caller need? Rather than prescribing a single fix, it maps common situations to the right construct so error handling reflects intent.

The SKILL.md provides thinking prompts, decision flowcharts, and quick-reference tables that route choices between `Result<T, E>`, `Option<T>`, the `?` operator, `panic!`/`assert!`, and the `thiserror` (library) versus `anyhow` (application) crates. It includes "trace up" guidance to related domain modules and "trace down" guidance to concrete implementations, plus tables of common errors and anti-patterns. A companion `examples/library-vs-app.md` walks through library error types with `thiserror`, application error handling with `anyhow` and `.context()`, layered error design, HTTP/CLI examples, and tests for error cases.

It targets intermediate Rust developers and teams who want consistent, idiomatic error strategy across libraries and applications. Typical use cases include deciding when to panic versus return a Result, designing typed error variants for a public crate boundary, adding context during propagation, and reviewing code for error-handling anti-patterns. The description also carries Chinese-language trigger phrases, signalling bilingual discoverability.

FAQ

What does this skill actually provide?

A reference SKILL.md plus an examples file. It offers decision tables, flowcharts, and thinking prompts for choosing Rust error-handling constructs; it does not ship runnable tooling or code that it executes on your behalf.

When should I use Result versus Option versus panic?

The skill's decision flowchart maps expected recoverable failures to Result<T, E>, normal absence to Option<T>, and bugs or invariant violations to panic!/assert!/unreachable!.

Does it recommend thiserror or anyhow?

Both, contextually: thiserror for typed errors at library boundaries so consumers can match on variants, and anyhow for ergonomic error handling with context inside applications.

Is any Rust experience assumed?

Yes. It assumes familiarity with Rust's Result/Option types and the ? operator, and is aimed at developers refining their error-handling strategy rather than absolute beginners.

Is the content available in languages other than English?

The trigger description includes Chinese phrases (for example 错误处理), but the body of the guidance itself is written in English.

All Files

3 files
examples/library-vs-app.md7.5 KB
View
patterns/error-patterns.md8.4 KB
View
SKILL.md4.4 KB
View

Install m06-error-handling

Download and extract the skill files to your .claude/skills/ directory.

Quick Setup:

  1. Copy the skill folder to .claude/skills/
  2. Claude will automatically detect and use the skill