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: StdError + 'static,
E: Into<Error<D>>;
fn with_context(self, context: impl Display) -> Result<T, Error<D>>;
fn unwrap_err_source<E>(self) -> E
where E: StdError + 'static;
}
Expand description
To use this trait on Result import the prelude use explicit_error::prelude::*
Required Methods§
Sourcefn try_map_on_source<F, S, E>(self, op: F) -> Result<T, Error<D>>
fn try_map_on_source<F, S, E>(self, op: F) -> Result<T, Error<D>>
Pattern match on the Error source from either the Error::Bug 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"))
),
}
})?;
Sourcefn with_context(self, context: impl Display) -> Result<T, Error<D>>
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::*, Bug};
Err::<(), _>(Bug::new()).with_context("Foo bar");
Sourcefn unwrap_err_source<E>(self) -> Ewhere
E: StdError + 'static,
fn unwrap_err_source<E>(self) -> Ewhere
E: StdError + 'static,
Unwrap and downcast the source of either Error::Domain or Error::Bug variant, panic otherwise. Usefull to assert_eq! in tests
§Examples
use explicit_error_exit::{ExitError, derive::ExitError, Error};
#[test]
fn test() {
asser_eq!(to_test().unwrap_err_source::<MyError>(), MyError::Foo);
}
#[derive(ExitError, Debug)]
enum MyError {
Foo,
}
fn to_test() -> Result<(), Error> {
Err(MyError::Foo)?;
Ok(())
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.