Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Handling

aspect-rs supports both Result and panic-based error handling.

With Result

#![allow(unused)]
fn main() {
#[aspect(LoggingAspect::new())]
fn may_fail(x: i32) -> Result<i32, Error> {
    if x < 0 {
        Err(Error::NegativeValue)
    } else {
        Ok(x * 2)
    }
}
}

The after_throwing advice is called when Err is returned.

With Panics

#![allow(unused)]
fn main() {
impl Aspect for PanicHandler {
    fn after_throwing(&self, ctx: &JoinPoint, error: &dyn Any) {
        if let Some(msg) = error.downcast_ref::<&str>() {
            eprintln!("Panic in {}: {}", ctx.function_name, msg);
        }
    }
}
}

See The Aspect Trait for more on after_throwing.