n0_error

Attribute Macro stack_error

#[stack_error]
Expand description

Attribute macro to expand error enums or structs.

This macro can be applied to enums and structs and the syntax #[stack_error(args)], where args is a comma-seperated list of arguments.

  • add_meta adds a n0_error::Meta field to the struct or each each enum variant. The Meta struct contains call-site location metadata for the error.
    • If the item has named fields, the field will added with name meta
    • If the item is a tuple, the field will be added as the last field and marked with #[error(meta)]
    • It the item is a unit, it will be altered to named fields, and the field will be added with name meta
  • derive will add #[derive(StackError)]. See the docs for the StackError for details.
  • The item-level options of StackError derive macro (namely from_sources and std_sources) can be set stack_error as well. They will be expanded to an #[error] attribute on the item. See the documentation for StackError for details.

This is an attribute macro because it modifies its item by adding the meta field, which derive macros cannot.

ยงExample

#[stack_error(derive, add_meta, from_sources)]
enum MyError {
    #[error("io failed")]
    Io { source: std::io::Error },
    #[error("remote failed with {_0}")]
    RemoteErrorCode(u32),
}

expands to

#[derive(n0_error::StackError)]
#[error(from_sources)]
enum MyError {
    #[error("io failed")]
    Io {
        source: std::io::Error,
        meta: n0_error::Meta,
    },
    #[error("remote failed with {_0}")]
    RemoteErrorCode(u32, #[error(meta)] n0_error::Meta),
}