Skip to main content

rethrow

Macro rethrow 

Source
macro_rules! rethrow {
    ($result:expr) => { ... };
    ($result:expr, $($args:tt)*) => { ... };
}
Expand description

Rethrow an Err, optionally with additional context

This is useful if the error path requires additional handling

Prelude import is required to bring in the Context trait.

use cu::pre::*;

fn some_fallable_func() -> cu::Result<String> {
    Ok("foo".to_string())
}

fn main() -> cu::Result<()> {
    // this input is just to show the formatting
    let input: i32 = 42;

    let foo = match some_fallable_func() {
        Ok(x) => x,
        Err(e) => {
            // supposed some additional handling is needed,
            // like setting some error state...

            cu::rethrow!(e, "failed: {input}");
        }
    };

    assert_eq!(foo, "foo");

    Ok(())
}