Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

error-battery-pack

Error handling done well. A battery pack that curates the essential error handling crates for Rust.

Quick Start

cargo bp add error

This adds anyhow and thiserror to your [dependencies] and sets up build-time validation.

When to Use Which

  • anyhow — Use in application code (binaries, CLI tools, servers) where you want to propagate errors with context and don’t need callers to match on specific variants.
  • thiserror — Use in library code where callers need to inspect and match on specific error variants.

They compose naturally: library functions return Result<T, MyError> (thiserror), and application code wraps them with anyhow::Result<T> adding .context().

Examples

Run the included examples to see the patterns in action:

# Basic anyhow usage with .context()
cargo run --example basic -p error-battery-pack

# Custom error types with thiserror + anyhow interop
cargo run --example custom-errors -p error-battery-pack

# Multi-layer error context chains
cargo run --example context-chain -p error-battery-pack

Acknowledgments

The skill files in skills/ are adapted from an error handling guide originally authored by @terminalwitchcraft.

License

Licensed under either of:

at your option.

Battery pack contents

NameDescription
anyhowFlexible concrete Error type built on std::error::Error
thiserrorderive(Error)