battery-pack-rs/battery-pack/library-errors
Claude Code and agent skill by battery-pack-rs/battery-pack
- community
Checking that the SKILL.md file is still in the repository.
Modeling, exposing, and formatting errors in Rust libraries
Install
npx vskill@latest install battery-pack-rs/battery-pack/library-errorsSKILL.md
Library Errors
How to model errors inside a crate, make them opaque at public boundaries, and control how they render.
Prerequisites: This skill references examples from the error battery pack.```shcargo install cargo-bp # install the battery-pack CLIcargo bp add error # add anyhow + thiserror to your project```
Internal Error Types
Within your crate boundary, use thiserror freely. No semver concerns since consumers never see these types.
Guidelines for internal errors:
- One enum per module or subsystem. Don't create a single god-error for the whole crate.
- Use `#[from]` liberally. It generates
Fromimpls so?works across internal boundaries. - Include diagnostic data in variants (offsets, keys, paths). You control the match sites, so adding fields is free.
- No `#[non_exhaustive]` needed. You own all the match arms.
Composing internal errors across modules
Public API Boundary
At the crate's public API, wrap internal errors in an opaque type. This decouples your internal structure from your consumers' code.
The Error+ErrorKind Pattern
This is the same pattern used by std::io::Error. Private fields, #[non_exhaustive] kind enum, boxed cause:
This gives semver freedom: private fields hide internals, #[non_exhaustive] allows adding variants, and the boxed cause hides dependency types. Copy on the kind lets .kind() return by value; if you later need variants carrying data, derive only Clone and return &MyLibErrorKind instead.
Converting internal errors to the public type
Map your rich internal taxonomy to the coarser public kinds. The internal error becomes the opaque .source().
Using thiserror at the boundary (middle ground)
thiserror with boxed sources gives derive convenience without exposing dependency types:
Use this only for crates with stable, well-understood error categories. For evolving APIs, prefer the struct pattern: any public enum has a larger semver surface than a struct (variant names, field arity, and tuple positions are all public API).
How consumers use your error type
Formatting Rules
Display: only this error's message
Display must not include the source. Reporters walk .source() separately:
Messages: lowercase, no trailing punctuation
Re-exporting Dependency Types
If consumers need to downcast to your dependency's error type, re-export it:
Prefer exposing information through your error kind or accessor methods over requiring consumers to downcast.
Checklist
- [ ] Internal errors:
thiserrorenum per module,#[from]for conversions - [ ] Public errors:
#[non_exhaustive], private fields, accessor methods - [ ]
Fromimpls map internal errors to public error kinds - [ ]
Box<dyn Error + Send + Sync + 'static>for causes (not concrete types) - [ ]
Displayprints only this error's message, never the source - [ ]
source()returns the underlying cause - [ ] Messages lowercase, no trailing punctuation
- [ ] Re-export dependency types only if consumers need to downcast
Related
See the application-errors skill for the consumer side: propagating errors with anyhow, formatting for logs and terminals, backtrace configuration.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingbattery-packs/error-battery-pack/skills/library-errors/SKILL.md
Popularity
- Repository stars
- 78
- Repository forks
- 12
- 7-day trend score
- 22
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/battery-pack-rs/battery-pack/library-errors)