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_metaadds an0_error::Metafield to the struct or each each enum variant. TheMetastruct 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
- If the item has named fields, the field will added with name
derivewill add#[derive(StackError)]. See the docs for theStackErrorfor details.- The item-level options of
StackErrorderive macro (namelyfrom_sourcesandstd_sources) can be setstack_erroras well. They will be expanded to an#[error]attribute on the item. See the documentation forStackErrorfor 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),
}