Skip to main content

catch

Function catch 

Source
pub fn catch<F, T>(f: F) -> Result<T, Error>
where F: FnOnce() -> Result<T, Error>,
Expand description

Catches panics and converts them to errors.

This function is useful for wrapping code that may panic, i.e., to shield against panics in user-defined code or third-party libraries. It captures the panic and returns it as an Error::Panic, allowing the program to continue running gracefully instead of terminating unexpectedly.

All of the function traits that we provide use this internally to ensure that any panic is caught and converted to an error. Thus, it’s absolutely recommended to wrap any user-defined function in this function when implementing a custom function trait.

§Errors

Returns Error::Panic if the provided function panics.

§Examples

use zrx_scheduler::action::Error;
use zrx_stream::function::catch;

// Define function that panics
let res = catch(|| {
    panic!("don't panic!");
    Ok(42) // Never returned
});

// Assert that panic was caught
assert!(matches!(res, Err(Error::Panic(_))));