Skip to main content

wrap_err

Attribute Macro wrap_err 

Source
#[wrap_err]
Expand description

Attribute macro that injects backtrace: std::backtrace::Backtrace and spantrace: tracing_error::SpanTrace into error types, and generates constructors that auto-capture both at the call site.

§Variant annotations (enum only)

  • #[leaf] — a freshly constructed error with no source. Injects backtrace+spantrace fields and generates a new_snake_case_name(…) constructor.

  • #[foreign] — wraps a foreign error type (Foreign(T) tuple variant). Converts to named fields { source: T, backtrace, spantrace }, adds #[error("{source}")] if no #[error(…)] is already present, and generates a From<T> impl that captures both backtrace and spantrace at the conversion site (?).

  • #[own] — wraps one of our own typed errors that already carries backtrace/spantrace (Own(InnerError) tuple variant). Adds #[error(transparent)] to the variant (if absent) and #[from] + #[backtrace] to the inner field, so thiserror’s provide() delegates to the source rather than capturing a new backtrace at the wrapping site.

§Struct usage

Injects backtrace+spantrace into the named fields and generates Self::new(…user_fields…).

§Example

#[wrap_err]
#[derive(Debug, thiserror::Error)]
#[error("something went wrong: {msg}")]
pub struct MyLeafError { msg: String }

#[wrap_err]
#[derive(Debug, thiserror::Error)]
pub enum MyError {
    #[leaf]
    #[error("bad value: {val}")]
    BadValue { val: String },

    #[foreign]
    Io(std::io::Error),

    #[foreign]
    #[error("parse error: {source}")]
    Parse(std::num::ParseIntError),

    #[own]
    Inner(MyLeafError),
}