Exemplo
Handle errors in a Rust application with anyhow: attach context, keep the cause chain, log it without losing the cause, and recover the concrete error
sha256:709971847660bb4f230ee832aa66da4263481bd8d5e0a498178ee3b789c53404
PUBLISHED
L3_CONTRACT_PASS
MIT-0
Caso
- Objetivo
- Handle errors in a Rust application with anyhow: attach context, keep the cause chain, log it without losing the cause, and recover the concrete error HOW
- Pacotes
- anyhow 1.0.104 thiserror 2.0.20
- Ambiente
- rust 1
- Criado
- 2026-08-14T08:53:52Z
Contrato
- assert the bare io::Error for a missing path is NotFound and its Display carries os error 2
- assert Display of a context-wrapped error prints only the outermost context, not the io message
- assert alternate Display {:#} flattens the whole chain into one colon-joined line while {} does not
- assert chain() yields both context frames then the original io error, in that order
- assert root_cause() returns the original io error at the bottom of the chain
- assert Debug {:?} prints the multi-line report with a Caused by section
- assert anyhow::Error derefs to a dyn StdError that prints as the outermost frame, and that source() through that Deref yields the next frame
- assert downcast_ref recovers the io::Error and its NotFound kind through two layers of context
- assert downcast_ref recovers a concrete thiserror library type out of a context-wrapped anyhow error
- assert anyhow::Error is one pointer wide, smaller than the concrete thiserror error, which is as wide as its String field
- assert bail! and ensure! build a one-frame error from a plain message with no source
- assert an interpolated bail! message downcasts to String while an anyhow! bare literal downcasts to &str
- assert io::Error and ParseIntError both convert through ? with no From impl written
- assert context() on a None Option produces an error with no source
- assert with_context never runs its closure on the success path while context() builds its message even when the Result is Ok
Arquivos
- Cargo.lock
- Cargo.toml
- csx.json
- src/main.rs
Seeder de origem
Recibos de verificação
- rust 1 · CONTAINER_RUN · compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS · cargo@1 · 2026-08-14 · ed25519:a2ec939a4c60e243
- · CONTAINER_RUN · compile:SKIPPED · contract:FAIL · load:SKIPPED · resolve:PASS · cargo@1 · 2026-08-14 · ed25519:7da3f610facce8bf