rust-error-handling
Skill0 starsChanged 3 days agoMITView on GitHub
Design and clean up Rust error handling with thiserror and anyhow - typed error enums for libraries (from/source chains, non_exhaustive, no stringly errors), anyhow with context for binaries, the ? operator, converting at crate boundaries, and replacing unwrap/expect/panic in production paths. Includes a scanner that skips test code and reports unwrap(), empty expect(), todo!/unimplemented!, anyhow or Box dyn Error in a library's public API, Result with String errors, map_err that discards the source and .ok() that swallows errors. Use when the user asks thiserror vs anyhow, how to define an error type, to remove unwraps, why an error message lost its cause, or to review error handling before a release.
6 other projects use a version of this.
Adopted by
Most recently taken up by
- swadhinbiswas/SKILLStheir own version, first seen Nov 22, 2026
- po4yka/rust-skillstheir own version, first seen Nov 15, 2026
- adxptived/Rust-Skillstheir own version, first seen Oct 17, 2026
- opensite-ai/opensite-skillstheir own version, first seen Oct 13, 2026
- botirkhaltaev/botir-skillstheir own version, first seen Jul 24, 2026
- codecharmhq/claude-code-skillstheir own version, first seen May 17, 2026
Used with
Projects with it also use rust-performance.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
huialiSkillrust-performance
Performance optimization expert covering profiling, benchmarking, memory allocation, SIMD, cache optimization, false sharing, lock contention, and NUMA-aware programming.
Also in 3 projects that use this
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- opensite-ai/opensite-skills★ 15same name
- po4yka/rust-skills★ 7same name
- botirkhaltaev/botir-skills★ 4same name
- adxptived/Rust-Skills★ 1same name
- codecharmhq/claude-code-skills★ 1same name
- swadhinbiswas/SKILLS★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository9 other files it gives its agents.
- AGENTS.mdAGENTS.md
- rust-async-reviewSkill
- rust-borrow-checkerSkill
- rust-crates-publishSkill
- rust-dependency-auditSkill
- rust-lint-ciSkill
- rust-testing-benchesSkill
- rust-unsafe-reviewSkill
- rust-workspace-setupSkill
The file
Files
Rust error handling
Goal: libraries return typed errors callers can match on; applications attach context and print a full cause chain; panics are reserved for broken invariants.
Paths below are relative to this skill's folder (in Claude Code: ${CLAUDE_SKILL_DIR}).
When to use
- "Should I use thiserror or anyhow?", "how do I define my own error type?"
- "Get rid of the unwraps", "this panics in production"
- Error messages without the underlying cause ("failed to load config" and nothing else)
- Reviewing a crate's public error API before publishing
When not to use
- A compile error about
?andFromconversions you need explained:rust-borrow-checker(diag_summary.pygives the E0277 hint), then come back here for the design - Panics inside
unsafecode or FFI:rust-unsafe-review
Workflow
- Classify the crate: library (others call it,
src/lib.rs), application (src/main.rs), or both. The rule: libraries -> thiserror enums, binaries -> anyhow. - Scan (read-only; tests, benches and examples are skipped):
In binariespython3 scripts/error_scan.py . # or a crate dir / file python3 scripts/error_scan.py . --json --fail-on high python3 scripts/error_scan.py . --min-severity mediumunwrap()is reported as low, in libraries as medium. A trailing// error-scan: allowcomment silences one line (for reviewed invariants). - Design the library error type with
references/patterns.md:
one enum per crate (or per module for large crates), variants named after what
failed,
#[from]only where the conversion is unambiguous,#[source]to keep the cause,#[non_exhaustive]on published enums. Do not put the source's message into#[error("...")]and also return it assource()(it prints twice). - Application side:
anyhow::Resultinmainand command handlers,.context()/.with_context(|| format!(...))at every I/O or parse boundary, print with{:#}or{:?}to get the chain, see references/patterns.md. - Replace unwraps one by one, choosing per site:
- can fail at runtime (I/O, parsing, user input) ->
?plus context or a variant - cannot fail by construction ->
expect("why it cannot fail") Option->ok_or(Error::Missing)?,ok_or_else(..), orlet Some(x) = .. else { .. }Show the diff per file; do not change public signatures without telling the user it is a breaking change.
- can fail at runtime (I/O, parsing, user input) ->
- Verify:
cargo check --all-targets,cargo test, then re-run the scanner. Optional clippy lints to keep it clean:unwrap_used,expect_used,panic,todo(see therust-lint-ciskill).
Decision points
| Question | Answer |
| Callers need to react differently to failures? | enum variants (thiserror) |
| Only reporting to a human? | anyhow (applications), or one opaque error struct in a library |
| Library wants to hide internals? | struct with private kind enum + kind() accessor |
| Error crosses threads / tasks? | make it Send + Sync + 'static (anyhow already is) |
Box<dyn Error> in a library? | replace; callers cannot match or downcast reliably |
no_std? | thiserror 2 supports no_std with default-features = false |
Output
error_scan: 14 findings -> 3 (reviewed invariants marked with error-scan: allow)
Library crate `parser`: new ParseError enum (Io, Syntax { line }, InvalidPort(ParseIntError))
Public API change: parse() now returns Result<Config, ParseError> (breaking, bump minor for 0.x)
Binary `cli`: anyhow with context on file reads; main prints the full chainDoes it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

