Macro manyhow::error_message

source ·
macro_rules! error_message {
    ($fmt:literal $($tt:tt)*) => { ... };
    ($span:expr, $fmt:literal $($tt:tt)*) => { ... };
}
Expand description

Creates an ErrorMessage, comparable to the anyhow! macro

If the first argument is not a literal it is taken as the span of the error. The span expression can either implement SpanRanged or implement ToTokens. Otherwise, Span::call_site is used.

assert_eq!(
    error_message!("format {} string{named}", "<3", named = "!").to_string(),
    "format <3 string!"
);
// Span can either be `proc_macro::Span` or `proc_macro2::Span`
assert_eq!(
    error_message!(Span::call_site(), "spanned error").to_string(),
    "spanned error"
);
// Or any expression implementing `quote::ToTokens`
assert_eq!(
    error_message!(quote!(some tokens), "spanned error").to_string(),
    "spanned error"
);

On top of the standard format_args! parameters additional attachments can be specified delimited with ;.

assert_eq!(
    error_message!(
        "format {} string{named}", "<3", named = "!";
        error = "some additional error";
        info = "some info as well";
        custom_attachment = "amazing"
    ).to_string(),
    "format <3 string!

  = error: some additional error
  = info: some info as well
  = custom_attachment: amazing
"
);