Skip to main content

ResultError

Trait ResultError 

Source
pub trait ResultError<T, D>
where D: Domain,
{ // Required methods fn try_map_on_source<F, S, E>(self, op: F) -> Result<T, Error<D>> where F: FnOnce(S) -> E, S: Error + 'static, E: Into<Error<D>>; fn with_context(self, context: impl Display) -> Result<T, Error<D>>; }
Expand description

To use this trait on Result import the prelude use explicit_error::prelude::*

Required Methods§

Source

fn try_map_on_source<F, S, E>(self, op: F) -> Result<T, Error<D>>
where F: FnOnce(S) -> E, S: Error + 'static, E: Into<Error<D>>,

Pattern match on the Error source from either the Error::Fault or Error::Domain variant if its type is the closure’s parameter type.

§Examples
    let err: Result<()> = Err(MyError::Foo)?;
     
    // Do the map if the source's type of the Error is MyError
    err.try_map_on_source(|e| {
        match e {
            MyError::Foo => HttpError::new(
                StatusCode::FORBIDDEN,
                ProblemDetails::new()
                    .with_type(Uri::from_static("/errors/forbidden"))
               ),
            MyError::Bar => HttpError::new(
                StatusCode::UNAUTHORIZED,
                ProblemDetails::new()
                    .with_type(Uri::from_static("/errors/unauthorized"))
               ),
        }
    })?;
Source

fn with_context(self, context: impl Display) -> Result<T, Error<D>>

Add a context to any variant of an Error wrapped in a Result::Err

§Examples
use explicit_error::{prelude::*, Fault};
Err::<(), _>(Fault::new()).with_context("Foo bar");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, D> ResultError<T, D> for Result<T, Error<D>>
where D: Domain, T: Debug,

Source§

fn try_map_on_source<F, S, E>(self, op: F) -> Result<T, Error<D>>
where F: FnOnce(S) -> E, S: Error + 'static, E: Into<Error<D>>,

Source§

fn with_context(self, context: impl Display) -> Result<T, Error<D>>

Implementors§