Skip to main content

Module err_ctx

Module err_ctx 

Source
Expand description

Substrate primitive over Result<T, E> for any E: [std::fmt:: Display] — the ONE substrate owner of the generic .map_err(|e| anyhow::anyhow!("<ctx>: {e}")) display-prefix wrap-shape for consumers whose source error is a bare Display type NOT already covered by a per-error-type flatten-wrap peer.

Peer of the type-specific flatten-wrap trait trio already in this crate on the same display-prefix wrap axis, partitioning the space by SPECIFICITY:

Pre-lift the shape was hand-authored at TWO tatara-export-worker/src/main.rs sites past the ★★ PRIME-DIRECTIVE ≥ 2 duplication threshold, both restating the SAME closure — capture an crate::export::ArtifactError returned by the substrate primitive crate::export::ArtifactSource::variant, prepend the identical static context slug "source", delegate the tail to std::fmt::Display via the {e} slot — differing in NOTHING but their line numbers. Post-lift both callsites read spec.source.variant().err_ctx("source")? and the wrap-shape lives at ONE substrate owner here.

§Naming — err_ctx, not context

Same discipline as the three specialized peers: the method name err_ctx is deliberately DISTINCT from anyhow::Context::context so a caller with anyhow::Context in scope can never resolve to the wrong method (which chain-wraps rather than display-prefix- flattens, and would silently drop the underlying error detail from every downstream tracing::error!(error = %e, ...) log line whose formatter interpolates {e} rather than the chain-walking {e:#}).

§Two flavors: err_ctx + err_ctx_with

  • ErrCtxExt::err_ctx takes a &'static str context — the most common shape ("source" at the two export-worker callsites). Static binding keeps the compile-time contract that the context slug is a bare literal, no allocation, no dynamic content leaking into an error stream downstream operators grep on.
  • ErrCtxExt::err_ctx_with takes an owned String context — the escape hatch for future consumers that compose the slug via format! (e.g. a dynamic per-variant-name slug the way crate::anyhow_flatten::FlattenCtxExt::flatten_ctx_with already services for phase_machine::evaluate_conditions).

§#[must_use]

Every consumer threads the ? short-circuit onto its handler’s anyhow::Result<_> return — dropping the wrap swallows the underlying failure entirely, which is never the intended semantic.

Theory anchor: THEORY.md §VI.1 (generation over composition — the generic display-prefix wrap-shape recurred at two byte-identical hand-authored sites in tatara-export-worker/src/main.rs past the ★★ PRIME-DIRECTIVE ≥ 2 duplication trigger, and is lifted to ONE substrate owner here). THEORY.md §II.1 invariant 5 (composition preserves proofs — a regression that drifts the display-prefix separator or the byte-shape surfaces here at the substrate pin rather than as silent operator-facing skew across every downstream err_ctx consumer).

Traits§

ErrCtxExt
Substrate extension trait over Result<T, E> for any E: std::fmt::Display — the ONE substrate owner of the generic .map_err(|e| anyhow::anyhow!("<ctx>: {e}")) display-prefix wrap-shape for consumers whose source error is a bare Display type NOT already covered by a per-error-type flatten-wrap peer. See the module docs for the specialized-peer partition + the naming rationale (why err_ctx and not context).