Skip to main content

expect_error_fn

Function expect_error_fn 

Source
pub fn expect_error_fn<F, T, E>(
    err: F,
    body: impl FnOnce() -> Result<T, Frame>,
) -> Result<T, E>
where F: FnOnce() -> E, E: WithContext,
Expand description

Low-level function for adding context with a custom error constructor.

This function is the foundation for other conversion functions. It allows complete control over how the final error is constructed.

§Type Parameters

  • F: A function that constructs the target error type E
  • T: The success type
  • E: The target error type (must implement WithContext)

§Example

use scoped_error::{expect_error_fn, WithContext, Frame};



fn do_work() -> Result<(), MyError> {
    let err = || MyError::new("custom");

    expect_error_fn(err, || {
        operation_that_might_fail()?;
        Ok(())
    })
}
Examples found in repository?
examples/multi.rs (lines 22-25)
19fn fetch(url: &str) -> Result<String, scoped_error::Error> {
20    let err = || scoped_error::Error::new(format!("failed to fetch {}", url));
21
22    expect_error_fn(err, || {
23        connect(url)?;
24        Ok("connected".into())
25    })
26}
27
28fn connect(url: &str) -> Result<String, scoped_error::Error> {
29    let err = || scoped_error::Error::new(format!("failed to connect to {}", url));
30
31    expect_error_fn(err, || Err("no network connection")?)
32}