#[derive(ModError)]
{
// Attributes available to this derive:
#[error_prefix]
#[error_display]
#[error_kind]
#[error_caption]
#[error_retryable]
#[error_http_status]
#[error_exit_code]
#[error_fatal]
}
Expand description
Derive macro for ModError
This macro automatically implements the ForgeError trait and common error handling functionality for a struct or enum, allowing for “lazy mode” error creation with minimal boilerplate.
§Example
When the macro is used in your application where error-forge is a dependency:
use error_forge::ModError;
#[derive(Debug, ModError)]
#[error_prefix("Database")]
pub enum DbError {
#[error_display("Connection to {0} failed")]
ConnectionFailed(String),
#[error_display("Query execution failed: {reason}")]
QueryFailed { reason: String },
#[error_display("Transaction error")]
#[error_http_status(400)]
TransactionError,
}#[error_display("...")] is optional; without it a variant displays
as its name. The string is a format! string and may reference any
subset of the variant’s fields: by name for struct-like variants
(a raw identifier such as r#type is written {type}), by position
({0}, {}) for tuple variants. Fields it does not mention are
ignored, {{ and }} produce literal braces, and a width or
precision given where the error is formatted ({:>20}) pads or
truncates the whole message.
§Attributes
On the enum (or struct):
#[error_prefix("...")]: caption prefix, giving"<prefix>: Error".
On each enum variant:
#[error_display("...")],#[error_kind("...")],#[error_caption("...")]: string literals.#[error_retryable],#[error_fatal]: flags. A bare flag meanstrue; an explicit value (#[error_retryable(false)]or#[error_retryable = false]) is honoured.#[error_http_status(404)]: an integer that fits inu16.#[error_exit_code(2)]: an integer that fits ini32.
Each attribute accepts both the list form (#[name(value)]) and the
name-value form (#[name = value]). A value of the wrong type or out
of range is a compile error.
§Structs
On a struct only #[error_prefix] is read. The struct displays as
"<prefix>: Error", its kind() is the struct name, and the other
metadata methods use the ForgeError defaults. The variant
attributes listed above (#[error_display], #[error_retryable],
and so on) are currently ignored when placed on a struct.
Note: This is a procedural macro that is re-exported by the error-forge crate.
When using in your application, import it from the main crate with use error_forge::ModError;.