🎯

m06-error-handling

🎯Skill

from goooice/rust-skills

VibeIndex|
What it does

Guides developers through strategic error handling in Rust, helping choose between Result, Option, panic, and context-aware error propagation techniques.

πŸ“¦

Part of

goooice/rust-skills(35 items)

m06-error-handling

Installation

Add MarketplaceAdd marketplace to Claude Code
/plugin marketplace add ZhangHanDong/rust-skills
Install PluginInstall plugin from marketplace
/plugin install rust-skills@rust-skills
Quick InstallInstall with npx
npx skills add ZhangHanDong/rust-skills
git cloneClone repository
git clone https://github.com/ZhangHanDong/rust-skills.git
πŸ“– Extracted from docs: goooice/rust-skills
4Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

"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"

Overview

# Error Handling

> Layer 1: Language Mechanics

Core Question

Is this failure expected or a bug?

Before choosing error handling strategy:

  • Can this fail in normal operation?
  • Who should handle this failure?
  • What context does the caller need?

---

Error β†’ Design Question

| Pattern | Don't Just Say | Ask Instead |

|---------|----------------|-------------|

| unwrap panics | "Use ?" | Is None/Err actually possible here? |

| Type mismatch on ? | "Use anyhow" | Are error types designed correctly? |

| Lost error context | "Add .context()" | What does the caller need to know? |

| Too many error variants | "Use Box" | Is error granularity right? |

---

Thinking Prompt

Before handling an error:

  1. What kind of failure is this?

- Expected β†’ Result

- Absence normal β†’ Option

- Bug/invariant β†’ panic!

- Unrecoverable β†’ panic!

  1. Who handles this?

- Caller β†’ propagate with ?

- Current function β†’ match/if-let

- User β†’ friendly error message

- Programmer β†’ panic with message

  1. What context is needed?

- Type of error β†’ thiserror variants

- Call chain β†’ anyhow::Context

- Debug info β†’ anyhow or tracing

---

Trace Up ↑

When error strategy is unclear:

```

"Should I return Result or Option?"

↑ Ask: Is absence/failure normal or exceptional?

↑ Check: m09-domain (what does domain say?)

↑ Check: domain-* (error handling requirements)

```

| Situation | Trace To | Question |

|-----------|----------|----------|

| Too many unwraps | m09-domain | Is the data model right? |

| Error context design | m13-domain-error | What recovery is needed? |

| Library vs app errors | m11-ecosystem | Who are the consumers? |

---

Trace Down ↓

From design to implementation:

```

"Expected failure, library code"

↓ Use: thiserror for typed errors

"Expected failure, application code"

↓ Use: anyhow for ergonomic errors

"Absence is normal (find, get, lookup)"

↓ Use: Option

"Bug or invariant violation"

↓ Use: panic!, assert!, unreachable!

"Need to propagate with context"

↓ Use: .context("what was happening")

```

---

Quick Reference

| Pattern | When | Example |

|---------|------|---------|

| Result | Recoverable error | fn read() -> Result |

| Option | Absence is normal | fn find() -> Option<&Item> |

| ? | Propagate error | let data = file.read()?; |

| unwrap() | Dev/test only | config.get("key").unwrap() |

| expect() | Invariant holds | env.get("HOME").expect("HOME set") |

| panic! | Unrecoverable | panic!("critical failure") |

Library vs Application

| Context | Error Crate | Why |

|---------|-------------|-----|

| Library | thiserror | Typed errors for consumers |

| Application | anyhow | Ergonomic error handling |

| Mixed | Both | thiserror at boundaries, anyhow internally |

Decision Flowchart

```

Is failure expected?

β”œβ”€ Yes β†’ Is absence the only "failure"?

β”‚ β”œβ”€ Yes β†’ Option

β”‚ └─ No β†’ Result

β”‚ β”œβ”€ Library β†’ thiserror

β”‚ └─ Application β†’ anyhow

└─ No β†’ Is it a bug?

β”œβ”€ Yes β†’ panic!, assert!

└─ No β†’ Consider if really unrecoverable

Use ? β†’ Need context?

β”œβ”€ Yes β†’ .context("message")

└─ No β†’ Plain ?

```

---

Common Errors

| Error | Cause | Fix |

|-------|-------|-----|

| unwrap() panic | Unhandled None/Err | Use ? or match |

| Type mismatch | Different error types | Use anyhow or From |

| Lost context | ? without context | Add .context() |

| cannot use ? | Missing Result return | Return Result<(), E> |

---

Anti-Patterns

| Anti-Pattern | Why Bad | Better |

|--------------|---------|--------|

| .unwrap() everywhere | Panics in production | .expect("reason") or ? |

| Ignore errors silently | Bugs hidden | Handle or propagate |

| panic! for expected errors | Bad UX, no recovery | Result |

| Box everywhere | Lost type info | thiserror |

---

Related Skills

| When | See |

|------|-----|

| Domain error strategy | m13-domain-error |

| Crate boundaries | m11-ecosystem |

| Type-safe errors | m05-type-driven |

| Mental models | m14-mental-model |

More from this repository10

🎯
m10-performance🎯Skill

Optimizes code performance by identifying bottlenecks, measuring impact, and guiding strategic improvements across algorithm, data structure, and memory efficiency.

🎯
m14-mental-model🎯Skill

Applies the M14 mental model framework to enhance decision-making and strategic thinking through structured cognitive analysis.

🎯
m04-zero-cost🎯Skill

Guides developers in choosing zero-cost abstractions by analyzing type system constraints and performance trade-offs in Rust generics and traits.

🎯
meta-cognition-parallel🎯Skill

Performs parallel three-layer meta-cognitive analysis by forking subagents to simultaneously analyze language mechanics, design choices, and domain constraints, then synthesizing results.

🎯
unsafe-checker🎯Skill

Identifies and reviews unsafe Rust code patterns, FFI risks, and potential memory unsafety in Rust projects.

🎯
rust-skill-creator🎯Skill

Dynamically generates Claude skills for Rust crates, standard library modules, and documentation by extracting and processing technical details from specified URLs.

🎯
coding-guidelines🎯Skill

Provides comprehensive Rust coding guidelines covering naming conventions, best practices, error handling, memory management, concurrency, and code style recommendations.

🎯
rust-refactor-helper🎯Skill

Performs safe Rust refactoring by analyzing symbol references, checking conflicts, and applying changes across project files using LSP.

🎯
m03-mutability🎯Skill

Diagnoses and guides resolution of Rust mutability and borrowing conflicts by analyzing ownership, mutation patterns, and thread-safety requirements.

🎯
m05-type-driven🎯Skill

Explores and demonstrates type-driven development techniques in Rust, showcasing advanced type system features and pattern matching strategies.