Skip to content
v0.1.1latest release34 skills2 categories4 sub-categories79 tested scripts157 reference docsContact us

Testing Errors

Reference for rust-error-handling-thiserror-anyhow-designer · 221 lines

Programming / Rustv1.0.0Updated OCT 21 20267 references9 scripts

Errors are API: test which variant is returned, what the message says, that the source chain is intact, that the type stays Send + Sync + 'static, and how the edge (CLI, HTTP) presents it. Examples use thiserror 2 and anyhow 1 unless marked std.

Contents

Asserting the variant

Prefer matches! over assert_eq!: it does not require PartialEq (which most source errors such as std::io::Error do not implement) and can check selected fields.

rust
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum StoreError {
    #[error("key {key:?} not found")]
    NotFound { key: String },
    #[error("i/o error on store log")]
    Io(#[from] std::io::Error),
}

pub fn get(map: &std::collections::HashMap<String, String>, key: &str) -> Result<String, StoreError> {
    map.get(key).cloned().ok_or_else(|| StoreError::NotFound { key: key.to_owned() })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn missing_key_is_not_found() {
        let err = get(&Default::default(), "a").unwrap_err();
        assert!(matches!(err, StoreError::NotFound { ref key } if key == "a"), "got {err:?}");
    }

    #[test]
    fn io_errors_convert() {
        let io = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied");
        let err: StoreError = io.into();
        assert!(matches!(&err, StoreError::Io(e) if e.kind() == std::io::ErrorKind::PermissionDenied));
    }
}

assert!(matches!(..), "got {err:?}") prints the actual error when the test fails, which a bare matches! does not.

Display messages

Messages are user-facing text; pin them so refactors do not change them by accident:

rust
#[test]
fn messages() {
    assert_eq!(StoreError::NotFound { key: "a".into() }.to_string(), r#"key "a" not found"#);
}

For many variants use snapshot testing (insta::assert_snapshot!(err.to_string())). Keep messages lowercase, without trailing punctuation, and without the source's text (see ERR009).

Source chains

Check that wrapping preserved the cause, and that no layer repeats it:

rust
use std::error::Error;

fn chain(e: &(dyn Error + 'static)) -> Vec<String> {
    let mut out = vec![e.to_string()];
    let mut cur = e.source();
    while let Some(s) = cur {
        out.push(s.to_string());
        cur = s.source();
    }
    out
}

#[derive(Debug, thiserror::Error)]
#[error("failed to load config")]
struct LoadError(#[source] std::io::Error);

#[test]
fn chain_has_each_layer_once() {
    let err = LoadError(std::io::Error::new(std::io::ErrorKind::NotFound, "no such file"));
    assert_eq!(chain(&err), ["failed to load config", "no such file"]);
}

anyhow errors: downcasting and context

rust
use anyhow::Context;

#[derive(Debug, thiserror::Error)]
#[error("rate limited")]
pub struct RateLimited;

fn call() -> anyhow::Result<()> {
    Err(RateLimited).context("calling the pricing service")
}

#[test]
fn context_and_root_cause() {
    let err = call().unwrap_err();
    assert_eq!(err.to_string(), "calling the pricing service");             // outermost context
    assert_eq!(format!("{err:#}"), "calling the pricing service: rate limited"); // whole chain
    assert!(err.downcast_ref::<RateLimited>().is_some());                     // typed root survives context
    assert!(err.root_cause().is::<RateLimited>());
}

downcast_ref::<T>() works through .context(..) layers for the original error type, which is how application code can still branch on a specific failure.

Compile-time properties

Lock in that errors can cross threads and convert into anyhow:

rust
const _: () = {
    const fn assert_error<E: std::error::Error + Send + Sync + 'static>() {}
    assert_error::<StoreError>();
};

Adding a Rc or a non-Send source to the enum later then fails to compile in your crate instead of in a downstream tokio::spawn.

Panics

rust
pub fn percent(part: u32, total: u32) -> u32 {
    assert!(total > 0, "total must be positive");
    part * 100 / total
}

#[test]
#[should_panic(expected = "total must be positive")]
fn zero_total_panics() {
    percent(1, 0);
}

expected matches a substring of the panic message. Use std::panic::catch_unwind only when testing behaviour after a panic (for example mutex poisoning).

HTTP error responses

With axum, drive the router with tower::ServiceExt::oneshot and assert on status and body:

rust
use axum::{body::Body, http::{Request, StatusCode}};
use tower::ServiceExt; // oneshot

#[tokio::test]
async fn unknown_item_is_404_with_code() {
    let app = app(test_state());
    let res = app.oneshot(Request::get("/items/999").body(Body::empty()).unwrap()).await.unwrap();
    assert_eq!(res.status(), StatusCode::NOT_FOUND);
    let body = axum::body::to_bytes(res.into_body(), 64 * 1024).await.unwrap();
    let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
    assert_eq!(json["code"], "NOT_FOUND");
}

#[tokio::test]
async fn internal_errors_do_not_leak() {
    let res = app(failing_state()).oneshot(Request::get("/items/1").body(Body::empty()).unwrap()).await.unwrap();
    assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
    let body = axum::body::to_bytes(res.into_body(), 64 * 1024).await.unwrap();
    assert!(!String::from_utf8_lossy(&body).contains("password")); // no internals in the body
}

CLI exit codes and stderr

Test the binary as a process with assert_cmd (and predicates):

rust
use assert_cmd::Command;
use predicates::str::contains;

#[test]
fn missing_config_exits_1_with_chain() {
    Command::cargo_bin("mytool").unwrap()
        .args(["--config", "/nope.toml"])
        .assert()
        .code(1)
        .stderr(contains("reading config /nope.toml"))
        .stderr(contains("No such file"));
}

fn main() -> anyhow::Result<()> exits with code 1 and prints Error: .. plus Caused by: lines (the Debug report) on failure.

Checklist

PropertyTest
Right variant for each failureassert!(matches!(err, E::X { .. }), "got {err:?}")
Stable messagesassert_eq!(err.to_string(), "..") or snapshots
Chain intact, no duplicateswalk source() and compare layers
Typed root survives anyhow contexterr.downcast_ref::<T>(), root_cause().is::<T>()
Send + Sync + 'staticconst-fn assertion
Panics only where documented#[should_panic(expected = "..")]
HTTP mapping and no leaksoneshot tests on status, code, body text
CLI behaviourassert_cmd: exit code and stderr
More in Rust

rust-borrow-checker-error-explainer

Explain and fix Rust borrow-checker, ownership, move and lifetime compiler errors: E0382 use of moved value, E0499 cannot borrow as mutable more than once, E0502 also borrowed as immutable, E0505, E0506, E0503, E0507 cannot move out of index/Rc/reference, E0515 return reference to local, E0597 does not live long enough, E0716 temporary value dropped while borrowed, E0373 closure may outlive (spawn needs move), E0521 borrowed data escapes, E0384, E0596, E0594, E0381, E0713, 'lifetime may not live long enough', RefCell 'already borrowed: BorrowMutError' panics. Ranks fixes from restructuring (split borrows, entry API, split_at_mut, get_disjoint_mut, mem::take, Option::take, indices, thread::scope) down to clone and Rc<RefCell>/Arc<Mutex>; never unsafe. Covers NLL limits (get-or-insert), iterator invalidation, closure captures, self-referential structs. Ships std-only Rust triage tools.

rust-cargo-workspace-designer

Design, review and fix Cargo workspaces: when to split a crate (compile time, boundaries, publishing, proc-macros), crates/* vs apps/ + crates/ layouts, virtual manifest vs root package, members/exclude/default-members, resolver 2 vs 3 (MSRV-aware, edition 2024), [workspace.package] and [workspace.dependencies] inheritance, [workspace.lints], root-only profiles (lto, codegen-units), additive features with dep: and weak ?/ syntax, feature unification pitfalls, internal path + version deps, publish = false, Cargo.lock and MSRV policy, xtask, CI with --locked, nextest, rust-cache, sccache, cargo-hack/deny/machete/udeps/semver-checks, and cargo build --timings. Ships a workspace auditor and crate-graph tool.

rust-lifetime-annotation-advisor

Decide when Rust lifetime annotations are needed and which ones: the three elision rules, structs holding references (struct Parser<'a>), owned vs borrowed (String, &str, Cow<'a, str>, Arc<str>), one vs several lifetimes, 'a: 'b bounds, T: 'static misconceptions, Box<dyn Trait + 'a> default object lifetimes, impl Trait + '_ and edition 2024 capture rules with use<..>, variance and PhantomData, HRTB for<'a> Fn(&'a T), GATs and lending iterators, async fn and tokio::spawn 'static, self-referential structs (ouroboros, self_cell, indices, arenas), Rc/Arc to escape lifetime soup. Ships std-only Rust checkers for redundant, missing, unused and over-restrictive lifetimes.

rust-tokio-async-service-builder

Build and review production async Rust services on Tokio 1.x and axum 0.8: runtime choice (#[tokio::main] multi_thread vs current_thread, Builder), axum routing with /{id} paths, State, extractors, IntoResponse error types, axum::serve with_graceful_shutdown, tower/tower-http middleware (TraceLayer, TimeoutLayer, CorsLayer, ConcurrencyLimitLayer, RequestBodyLimitLayer), Arc<AppState>, std vs tokio Mutex, JoinSet, TaskTracker, CancellationToken, select! cancel safety, bounded mpsc, oneshot, broadcast, watch, actors, Semaphore, timeouts, retries, spawn_blocking, sqlx PgPool, reqwest Client reuse, tracing, tests with start_paused and oneshot, Dockerfile. Fixes 'future cannot be sent between threads safely', E0373, Rc or MutexGuard held across await, 'Cannot start a runtime from within a runtime', 'Path segments must not start with :'. Ships std-only Rust scanners and a compiling axum 0.8 scaffold.

rust-trait-design-reviewer

Review and design Rust traits and generic APIs: generics vs impl Trait vs dyn Trait (static vs dynamic dispatch), dyn compatibility / object safety (generic methods, Self returns, where Self: Sized, error[E0038] not dyn compatible / cannot be made into an object), associated types vs generic parameters, blanket impls, coherence and orphan rule (E0117, E0119, E0210), newtype, extension, sealed and marker traits, supertraits and trait upcasting (1.86), async fn in traits (1.75) and the Send bound problem (trait_variant, async-trait), RPITIT, GATs, From/Into/TryFrom/AsRef/Borrow, std trait checklist (Debug, Clone, PartialEq, Hash, Default, Display, Error, Send/Sync, serde feature), #[must_use], #[non_exhaustive], semver hazards, Deref abuse, builders, typestate, PhantomData, Box<dyn Error + Send + Sync>, Fn traits, mockall.

rust-unsafe-block-auditor

Audit unsafe Rust for soundness: inventory every unsafe block, unsafe fn, unsafe impl/trait, extern block and unsafe attribute, check // SAFETY: comments and # Safety docs, and classify each as sound, needs-justification or likely UB, proposing safe alternatives first. Covers the five unsafe superpowers, aliasing (Stacked/Tree Borrows), invalid values, MaybeUninit vs mem::zeroed/uninitialized, transmute, slice::from_raw_parts, Vec::set_len/from_raw_parts, get_unchecked, CString::as_ptr dangling, unsafe impl Send/Sync, Pin projection, FFI (extern "C", repr(C), C-unwind, Box::into_raw/from_raw), strict provenance, static mut and &raw, edition 2024 (unsafe extern, #[unsafe(no_mangle)], unsafe_op_in_unsafe_fn, env::set_var), Miri, sanitizers, cargo-fuzz, loom, cargo-geiger and clippy safety lints.