Skip to main content

msg_error_anyhow

Macro msg_error_anyhow 

Source
macro_rules! msg_error_anyhow {
    ($msg:expr) => { ... };
}
Expand description

Creates an anyhow::Error from a message with ❌ prefix.

This macro provides a convenient way to create anyhow::Error instances from application messages. It’s useful for error propagation in functions that return Result<T, anyhow::Error> and need to convert application messages into proper error types.

§Error Creation Strategy

  • Prefix Addition: Automatically adds ❌ prefix for visual consistency
  • Error Propagation: Creates errors suitable for ? operator use
  • Message Integration: Works with the application’s message system
  • Type Compatibility: Returns anyhow::Error for easy integration

§Use Cases

§Function Error Returns

use anyhow::Result;
use kasl::{msg_error_anyhow, libs::messages::Message};

fn validate_config() -> Result<()> {
    if config_is_invalid() {
        return Err(msg_error_anyhow!(Message::ConfigParseError));
    }
    Ok(())
}

§Error Context Addition

use anyhow::{Result, Context};
use kasl::{msg_error_anyhow, libs::messages::Message};

fn complex_operation() -> Result<()> {
    some_operation()
        .context(msg_error_anyhow!(Message::TaskUpdateFailed))
}

§Error Handling Benefits

  • Consistent Formatting: All errors have consistent visual presentation
  • Message Reuse: Leverages existing message definitions
  • Type Safety: Provides proper error types for Rust’s error handling
  • Integration: Works seamlessly with anyhow and ? operator