rust-error-handling
Skill7 starsChanged 30 days agoBSD-3-ClauseView on GitHub
Use when designing, propagating, or reporting Rust errors, or when rustc says "`?` couldn't convert the error" (E0277), "is not satisfied" for std::error::Error, or E0599 on .context. Covers thiserror enums with `#[from]` and `#[source]`, the Error::source chain, anyhow::Context, Box<dyn Error + Send + Sync>, one error enum per module or per crate, dependency error types in a public API, printing an error chain for users and logs, stable error codes for machines, and ExitCode or Termination in main. Not for FFI error mapping (ffi-error-progress-cancel).
6 other projects use a version of this.
Adopted by
Most recently taken up by
- saizul/rust-dev-skillstheir own version, first seen Nov 29, 2026
- swadhinbiswas/SKILLStheir own version, first seen Nov 22, 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
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
- botirkhaltaev/botir-skills★ 4same name
- adxptived/Rust-Skills★ 1same name
- codecharmhq/claude-code-skills★ 1same name
- swadhinbiswas/SKILLS★ 1same name
- saizul/rust-dev-skills★ 0same 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 repository51 other files it gives its agents.
- AGENTS.mdAGENTS.md
- cargo-workflowsSkill
- ffi-error-progress-cancelSkill
- memory-modelSkill
- rust-allocatorsSkill
- rust-android-buildSkill
- rust-async-internalsSkill
- rust-borrow-semanticsSkill
- rust-callback-boundsSkill
- rust-cliSkill
- rust-code-styleSkill
- rust-compiler-errorsSkill
- rust-copy-on-writeSkill
- rust-crate-architectureSkill
- rust-crate-releaseSkill
- rust-cross-compilationSkill
- rust-cxxSkill
- rust-databaseSkill
- rust-debuggingSkill
- rust-disciplineSkill
- rust-embedded-no-stdSkill
- rust-event-loop-stateSkill
- rust-hot-pathSkill
- rust-ios-buildSkill
- rust-iterator-implSkill
- rust-jniSkill
- rust-lintsSkill
- rust-macrosSkill
- rust-native-linkingSkill
- rust-networkingSkill
- rust-observabilitySkill
- rust-panic-safetySkill
- rust-pattern-semanticsSkill
- rust-performanceSkill
- rust-pin-projectionSkill
- rust-portSkill
- rust-pyo3Skill
- rust-reviewSkill
- rust-sanitizers-miriSkill
- rust-securitySkill
- rust-send-syncSkill
- rust-serdeSkill
- rust-swift-ffiSkill
- rust-tddSkill
- rust-test-toolsSkill
- rust-type-erasureSkill
- rust-unsafeSkill
- rust-varianceSkill
- rust-wasmSkill
- uniffi-boundarySkill
- uniffi-packaging-versioningSkill
The file
We split this skill into the rules it gives an agent
6 rules. 0 are common, 2 are distinctive, and 4 are very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 71
Never put anyhow::Error or a bare Box<dyn Error> in the public signature of a library.
- Distinctive: line 79
Do not build a crate-wide enum in which each function produces 2 of 15 variants.
- Very specific: line 80
Context lives in fields: Put the data a reader needs (path, key, id) in the variant, and the cause in a #[source] field.
- Distinctive: line 82
Display: Write a short lowercase message with no trailing period, and leave the source out of it.
- Very specific: line 84
Thread-safe: Keep every error Send + Sync + 'static.
- Very specific: line 180
Read references/main-and-logs.md when you write main, map an error to an exit status, print a chain without anyhow, or log an error.
“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Rust Error Handling
This skill owns error-type design and propagation in a library and in an application: the
type a function returns, the source chain, context, conversion with ?, and how the program
reports a failure. Facts about crates are for thiserror 2.0.21 and anyhow 1.0.104 on Rust
1.99.1.
Other skills own the neighbors. The rust-discipline skill owns unwrap and expect and the
#[non_exhaustive] rule. The rust-code-style skill owns the rustdoc # Errors section. The
rust-cli skill owns the process exit-code table and stderr output. The rust-panic-safety
skill owns panics at an FFI boundary; the ffi-error-progress-cancel skill owns error shapes
across FFI. Install those skills, or, without them, map each error at an FFI boundary to a
stable code() string and catch each panic with std::panic::catch_unwind.
Gotchas
- A
Displaymessage that includes its source prints the source twice in a chain.#[error("io error: {0}")] Io(#[from] io::Error)printsio error: no such file: no such fileunder anyhow{:#}. Return the cause fromsource()or print it inDisplay, never both. ?callsFrom::fromonce. It does not chainA -> B -> C, even when both impls exist..map_err(|_| Error::Io)drops the cause. Keep the original error as the#[source]field.- thiserror rejects
#[from]on a variant with another field (deriving From requires no fields other than source and backtrace). Use#[source]andmap_errwhen the variant also carries context, such as a path. anyhow::Errorneeds aSend + Sync + 'staticsource;std::thread::spawnandtokio::spawnneed aSend + 'staticresult. ABox<dyn Error>withoutSend + Sync, or anRcin a field, fails with E0277.anyhow::Errordoes not implementstd::error::Error. It derefs todyn Error + Send + Sync + 'staticand converts intoBox<dyn Error + Send + Sync>.err.downcast_ref::<E>()on ananyhow::Errorsees through.context()layers, but not through a thiserror#[source]field. Search the chain:err.chain().find_map(|c| c.downcast_ref::<E>()).anyhow::Error::from_boxed(boxed)andanyhow!(boxed)keep the box: a laterdowncast_ref::<io::Error>()returnsNone, and so does a chain search. Downcast the box before you wrap it.fn main() -> Result<(), E>printsError: {err:?}and exits with status 1. For anyhow that is theCaused by:list, plus a backtrace whenRUST_BACKTRACE=1is set.
Triage
| Symptom | Cause | Fix |
E0277: `?` couldn't convert the error to `AppError` with note: 'AppError' needs to implement 'From<io::Error>' | No From impl for the source error | Add #[from] to a variant, or map_err into a variant with #[source] |
The same E0277 with help: for that trait implementation, expected 'B', found 'ParseIntError' | A two-hop conversion | Convert the first hop: .map_err(B::from)? |
`?` couldn't convert the error: `String: std::error::Error` is not satisfied | ? into anyhow::Error from String | Return a real error type; at the call site, .map_err(anyhow::Error::msg)?. The std boxed error types accept String directly |
`?` couldn't convert the error: `dyn std::error::Error + Send + Sync: Sized` is not satisfied | ? from a Box<dyn Error + Send + Sync> into anyhow::Error | .map_err(anyhow::Error::from_boxed)?, or return a concrete error type |
E0599: no method named `context` found for enum `Result<T, E>` | The anyhow::Context trait is not in scope | use anyhow::Context as _; |
E0599: the method `context` exists for enum `Result<u8, String>`, but its trait bounds were not satisfied | The error type does not implement std::error::Error | .map_err(anyhow::Error::msg).context(..) |
E0277: the trait bound `anyhow::Error: std::error::Error` is not satisfied | A generic E: Error parameter receives an anyhow::Error | Take &(dyn Error + 'static) and pass err.as_ref() |
E0277: `dyn std::error::Error` cannot be sent between threads safely | Box<dyn Error> crosses a thread or task | Use Box<dyn Error + Send + Sync + 'static> |
E0282 type annotations needed on ? in a closure or an async block | Nothing names the error type of ? | End with Ok::<_, AppError>(value) |
the `?` operator can only be used on `Result`s, not `Option`s | ? on an Option in a Result function | .ok_or(Error::Missing)?, or .context("..")? with anyhow |
E0119: conflicting implementations of trait `From<std::io::Error>` | Two variants have #[from] of one type | Keep #[from] on one; map_err into the other |
E0599: the method `as_dyn_error` exists for ... from a thiserror derive | A #[source] or #[from] field, or a field named source, is not an error | Rename the field, or wrap the value in an error type |
clippy::result_large_err | The Err type is 128 bytes or more | Box the large variant's payload; set large-error-threshold in clippy.toml only with a reason |
Choose the error type
| Code | Default | Reason |
| Library public API | A thiserror enum or struct | The caller can match the cases it handles |
| Application, binary, test helper | anyhow::Result with .context() | The program reports the error; it does not match on it |
| Library callback or plugin result whose type the library cannot know | Box<dyn Error + Send + Sync + 'static> as a #[source] field | Keeps the chain, and downcast_ref reaches the concrete type |
Never put anyhow::Error or a bare Box<dyn Error> in the public signature of a library. The
caller can then only print the message or guess a type to downcast to.
Design a library error
- Granularity. Give each variant one failure that a caller handles differently. Default: one enum per module or per operation family, so a function returns only variants it can produce. Use one enum per crate when the crate is small or its operations share most failures. Do not build a crate-wide enum in which each function produces 2 of 15 variants.
- Context lives in fields. Put the data a reader needs (path, key, id) in the variant, and
the cause in a
#[source]field. thiserror 2.0.21 formats aPathBuffield with{path}. - Display. Write a short lowercase message with no trailing period, and leave the source out of it. This is the std convention and a catalog default.
- Thread-safe. Keep every error
Send + Sync + 'static. Assert it once per public type. #[non_exhaustive]on every public error enum from its first release. Therust-disciplineskill owns that rule.- Dependency types. A
#[from] dep::Errorfield or apubimpl From<dep::Error>makes the dependency's major version part of your API. Readreferences/library-errors.mdwhen a public error holds or converts a third-party error, when you want an opaque error with akind(), or when you add error codes.
use std::path::{Path, PathBuf};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
#[error("cannot read the config file {path}")]
Read {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("invalid port {value:?}")]
Port {
value: String,
#[source]
source: std::num::ParseIntError,
},
}
// Compile-time check that the error can cross threads and enter anyhow.
const _: () = {
const fn assert_error<T: std::error::Error + Send + Sync + 'static>() {}
assert_error::<ConfigError>();
};
pub fn load_port(path: &Path) -> Result<u16, ConfigError> {
let text = std::fs::read_to_string(path)
.map_err(|source| ConfigError::Read { path: path.to_path_buf(), source })?;
let value = text.trim();
value.parse().map_err(|source| ConfigError::Port { value: value.to_owned(), source })
}Propagate and add context
? converts the error with From::from. When no impl exists, the build fails:
#[derive(Debug)]
pub struct AppError;
pub fn read() -> Result<String, AppError> {
// `?` couldn't convert the error to `AppError`
let text = std::fs::read_to_string("app.toml")?;
Ok(text)
}In an application, add context at each layer with anyhow::Context. .context() takes a
value; .with_context(|| ..) formats only on the error path. Both keep the source, and both
also work on an Option.
use anyhow::Context as _;
use std::path::Path;
pub fn port(path: &Path) -> anyhow::Result<u16> {
let text = std::fs::read_to_string(path)
.with_context(|| format!("cannot read {}", path.display()))?;
let line = text.lines().next().context("the file is empty")?;
line.trim().parse().with_context(|| format!("invalid port {line:?}"))
}
pub fn is_missing(err: &anyhow::Error) -> bool {
// The context layer keeps the source, so downcast reaches the io::Error.
err.downcast_ref::<std::io::Error>()
.is_some_and(|io| io.kind() == std::io::ErrorKind::NotFound)
}? converts every E: Error + Send + Sync + 'static, a &str, and a String into
Box<dyn Error + Send + Sync + 'static>. Keep 'static: downcast_ref needs it.
Report the error
- Report once, at the boundary that decides what happens next:
main, a request handler, a task supervisor. Default: a layer either returns the error or logs it, not both, because a log-and-return prints one failure several times. - For a person, print the whole chain on one line:
{err:#}foranyhow::Error, or a loop oversource()for any other error.{err}prints only the outer message. - For a machine, expose a stable code, not the message. Match on a variant or a
code()string, never onDisplaytext, which a wording fix changes. - In
main, returnstd::process::ExitCode, or a type that implementsstd::process::Termination. Therust-cliskill owns which status means what.std::error::Reportis unstable as of Rust 1.99.1 (error_reporter).
Read references/main-and-logs.md when you write main, map an error
to an exit status, print a chain without anyhow, or log an error.
Verify
| Claim | Check | What a green result does not prove |
Every ? converts | cargo check --all-targets --locked | That the conversion kept the source |
| The source chain is complete | A test that walks source() and asserts each layer's message, or format!("{err:#}") for anyhow | Other error paths than the one the test triggers |
| No message repeats its source | The same chain test: each cause appears once | |
The error is Send + Sync + 'static | The const assertion above compiles | Nothing about errors inside a Box<dyn Error> field |
| A public error change keeps SemVer | cargo semver-checks check-release, if it is installed | A dependency type in a public field or From impl; review it by hand |
The Err type stays small | cargo clippy --all-targets --locked -- -D warnings (result_large_err) | Size on the success path |
Does 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.

