Design actionable errors in a Rust CLI
8 steps · about 4 h 15 min · Intermediate
- Separate recoverable errors from bugs (guide, Rust By Example, 30 min)
Result and panic communicate fundamentally different failure contracts.
- Propagate failures with Result (guide, Rust By Example, 40 min)
Typed errors keep success and failure visible through every caller.
- Preserve an error source chain (reference, Rust standard library, 30 min)
Source relationships retain the underlying cause while a boundary adds meaning.
- Return an intentional process status (reference, Rust standard library, 25 min)
Automation needs a stable success or failure signal independent of prose.
- Define focused library error variants (guide, thiserror documentation, 30 min)
Derived implementations reduce boilerplate without erasing the public error model.
- Add application context at the boundary (guide, anyhow documentation, 30 min)
A CLI can enrich failures for people while libraries retain typed contracts.
- Validate command-line inputs before execution (guide, clap documentation, 35 min)
Parser-level validation gives consistent usage errors before domain work begins.
- Test stderr and exit status together (guide, assert_cmd documentation, 35 min)
End-to-end assertions protect both scripting behaviour and actionable messages.