Design actionable errors in a Rust CLI

8 steps · about 4 h 15 min · Intermediate

  1. Separate recoverable errors from bugs (guide, Rust By Example, 30 min)

    Result and panic communicate fundamentally different failure contracts.

  2. Propagate failures with Result (guide, Rust By Example, 40 min)

    Typed errors keep success and failure visible through every caller.

  3. Preserve an error source chain (reference, Rust standard library, 30 min)

    Source relationships retain the underlying cause while a boundary adds meaning.

  4. Return an intentional process status (reference, Rust standard library, 25 min)

    Automation needs a stable success or failure signal independent of prose.

  5. Define focused library error variants (guide, thiserror documentation, 30 min)

    Derived implementations reduce boilerplate without erasing the public error model.

  6. Add application context at the boundary (guide, anyhow documentation, 30 min)

    A CLI can enrich failures for people while libraries retain typed contracts.

  7. Validate command-line inputs before execution (guide, clap documentation, 35 min)

    Parser-level validation gives consistent usage errors before domain work begins.

  8. Test stderr and exit status together (guide, assert_cmd documentation, 35 min)

    End-to-end assertions protect both scripting behaviour and actionable messages.