Skip to main content

with_error_scope

Function with_error_scope 

Source
pub fn with_error_scope<S, W, C, T, R, E>(
    setup: S,
    work: W,
    cleanup: C,
) -> Result<R, E>
where S: FnOnce() -> Result<T, E>, W: FnOnce(&T) -> Result<R, E>, C: FnOnce(T),
Expand description

Convenience function for scope-aware error handling

This function allows you to handle errors within a scoped operation while still ensuring cleanup happens.

ยงExample

use foundation_utils::scoped::with_error_scope;

let result = with_error_scope(
    || Ok("resource"),
    |resource| {
        // Work that might fail
        if resource.len() > 0 {
            Ok(format!("processed: {}", resource))
        } else {
            Err("empty resource")
        }
    },
    |resource| println!("cleanup: {}", resource)
);