Trait snafu::futures::try_future::TryFutureExt

source ·
pub trait TryFutureExt: TryFuture + Sized {
    // Required methods
    fn context<C, E>(self, context: C) -> Context<Self, C, E> 
       where C: IntoError<E, Source = Self::Error>,
             E: Error + ErrorCompat;
    fn with_context<F, C, E>(self, context: F) -> WithContext<Self, F, E> 
       where F: FnOnce(&mut Self::Error) -> C,
             C: IntoError<E, Source = Self::Error>,
             E: Error + ErrorCompat;
    fn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E> 
       where S: Into<String>,
             E: FromString;
    fn with_whatever_context<F, S, E>(
        self,
        context: F,
    ) -> WithWhateverContext<Self, F, E> 
       where F: FnOnce(&mut Self::Error) -> S,
             S: Into<String>,
             E: FromString;
}
Expand description

Additions to [TryFuture].

Required Methods§

source

fn context<C, E>(self, context: C) -> Context<Self, C, E>
where C: IntoError<E, Source = Self::Error>, E: Error + ErrorCompat,

Extend a [TryFuture]’s error with additional context-sensitive information.

use futures::future::TryFuture;
use snafu::prelude::*;

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

fn example() -> impl TryFuture<Ok = i32, Error = Error> {
    another_function().context(AuthenticatingSnafu {
        user_name: "admin",
        user_id: 42,
    })
}

fn another_function() -> impl TryFuture<Ok = i32, Error = 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, E>(self, context: F) -> WithContext<Self, F, E>
where F: FnOnce(&mut Self::Error) -> C, C: IntoError<E, Source = Self::Error>, E: Error + ErrorCompat,

Extend a [TryFuture]’s error with lazily-generated context-sensitive information.

use futures::future::TryFuture;
use snafu::prelude::*;

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

fn example() -> impl TryFuture<Ok = i32, Error = Error> {
    another_function().with_context(|_| AuthenticatingSnafu {
        user_name: "admin".to_string(),
        user_id: 42,
    })
}

fn another_function() -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

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

source

fn whatever_context<S, E>(self, context: S) -> WhateverContext<Self, S, E>
where S: Into<String>, E: FromString,

Extend a [TryFuture]’s error with information from a string.

The target error type must implement FromString by using the #[snafu(whatever)] attribute. The premade Whatever type is also available.

In many cases, you will want to use with_whatever_context instead as it is only called in case of error. This method is best suited for when you have a string literal.

use futures::future::TryFuture;
use snafu::{prelude::*, Whatever};

fn example() -> impl TryFuture<Ok = i32, Error = Whatever> {
    api_function().whatever_context("The API failed")
}

fn api_function() -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}
source

fn with_whatever_context<F, S, E>( self, context: F, ) -> WithWhateverContext<Self, F, E>
where F: FnOnce(&mut Self::Error) -> S, S: Into<String>, E: FromString,

Extend a [TryFuture]’s error with information from a lazily-generated string.

The target error type must implement FromString by using the #[snafu(whatever)] attribute. The premade Whatever type is also available.

use futures::future::TryFuture;
use snafu::{prelude::*, Whatever};

fn example(arg: &'static str) -> impl TryFuture<Ok = i32, Error = Whatever> {
    api_function(arg)
        .with_whatever_context(move |_| format!("The API failed for argument {arg}"))
}

fn api_function(arg: &'static str) -> impl TryFuture<Ok = i32, Error = ApiError> {
    /* ... */
}

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<Fut> TryFutureExt for Fut
where Fut: TryFuture,