Trait snafu::ResultExt

source ·
pub trait ResultExt<T, E>: Sized {
    // Required methods
    fn context<C, E2>(self, context: C) -> Result<T, E2>
       where C: IntoError<E2, Source = E>,
             E2: Error + ErrorCompat;
    fn with_context<F, C, E2>(self, context: F) -> Result<T, E2>
       where F: FnOnce() -> C,
             C: IntoError<E2, Source = E>,
             E2: Error + ErrorCompat;
}
Expand description

Additions to Result.

Required Methods§

source

fn context<C, E2>(self, context: C) -> Result<T, E2>where C: IntoError<E2, Source = E>, E2: Error + ErrorCompat,

Extend a Result’s error with additional context-sensitive information.

use snafu::{ResultExt, Snafu};

#[derive(Debug, Snafu)]
enum Error {
    Authenticating {
        user_name: String,
        user_id: i32,
        source: ApiError,
    },
}

fn example() -> Result<(), Error> {
    another_function().context(Authenticating {
        user_name: "admin",
        user_id: 42,
    })?;
    Ok(())
}

fn another_function() -> Result<i32, ApiError> {
    /* ... */
}

Note that the context selector will call Into::into on each field, so the types are not required to exactly match.

source

fn with_context<F, C, E2>(self, context: F) -> Result<T, E2>where F: FnOnce() -> C, C: IntoError<E2, Source = E>, E2: Error + ErrorCompat,

Extend a Result’s error with lazily-generated context-sensitive information.

use snafu::{ResultExt, Snafu};

#[derive(Debug, Snafu)]
enum Error {
    Authenticating {
        user_name: String,
        user_id: i32,
        source: ApiError,
    },
}

fn example() -> Result<(), Error> {
    another_function().with_context(|| Authenticating {
        user_name: "admin".to_string(),
        user_id: 42,
    })?;
    Ok(())
}

fn another_function() -> Result<i32, ApiError> {
    /* ... */
}

Note that this may not be needed in many cases because the context selector will call Into::into on each field.

Implementations on Foreign Types§

source§

impl<T, E> ResultExt<T, E> for Result<T, E>

source§

fn context<C, E2>(self, context: C) -> Result<T, E2>where C: IntoError<E2, Source = E>, E2: Error + ErrorCompat,

source§

fn with_context<F, C, E2>(self, context: F) -> Result<T, E2>where F: FnOnce() -> C, C: IntoError<E2, Source = E>, E2: Error + ErrorCompat,

Implementors§