Trait ResultBug

Source
pub trait ResultBug<T, S> {
    // Required methods
    fn map_err_or_bug<F, E, D>(self, op: F) -> Result<T, Error<D>>
       where F: FnOnce(S) -> Result<E, S>,
             E: Into<Error<D>>,
             S: StdError + 'static,
             D: Domain;
    fn bug(self) -> Result<T, Bug>;
    fn bug_with_source(self) -> Result<T, Bug>
       where S: StdError + 'static;
    fn bug_force(self) -> Result<T, Bug>;
    fn bug_force_with_source(self) -> Result<T, Bug>
       where S: StdError + 'static;
}
Expand description

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

Required Methods§

Source

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

Convert with a closure any error wrapped in a Result to an Error. Returning an Ok convert the wrapped type to Error::Domain. Returning an Err generates a Bug with the orginal error has its source.

§Examples

Pattern match to convert to an Error::Domain

fn authz_middleware(public_identifier: &str) -> Result<(), Error> {
    let entity = fetch_bar(&public_identifier).map_err_or_bug(|e|
        match e {
            sqlx::Error::RowNotFound => Ok(
                NotFoundError::Bar(
                    public_identifier.to_string())),
            _ => Err(e), // Convert to Error::Bug
        }
    )?;

    Ok(entity)
}
Source

fn bug(self) -> Result<T, Bug>

Convert any Result::Err into a Result::Err wrapping a Bug

fn foo() -> Result<(), Error> {
   let file: Result<File, std::io::Error> = File::open("foo.conf");
   file.bug().with_context("Configuration file foo.conf is missing.")?;

   Err("error message").bug()?;
}
Source

fn bug_with_source(self) -> Result<T, Bug>
where S: StdError + 'static,

Same behavior as bug but capture the original error as a source. Only applicable to wrapped std::errror::Error

Source

fn bug_force(self) -> Result<T, Bug>

Convert any Result::Err into a Result::Err wrapping a Bug forcing backtrace capture

fn foo() -> Result<(), Error> {
   let file: Result<File, std::io::Error> = File::open("foo.conf");
   file.bug_force().with_context("Configuration file foo.conf is missing.")?;
}
Source

fn bug_force_with_source(self) -> Result<T, Bug>
where S: StdError + 'static,

Same behavior as bug_force but capture the original error as a source. Only applicable to wrapped std::errror::Error

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.

Implementations on Foreign Types§

Source§

impl<T, S> ResultBug<T, S> for Result<T, S>

Source§

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

Source§

fn bug(self) -> Result<T, Bug>

Source§

fn bug_force(self) -> Result<T, Bug>

Source§

fn bug_with_source(self) -> Result<T, Bug>
where S: StdError + 'static,

Source§

fn bug_force_with_source(self) -> Result<T, Bug>
where S: StdError + 'static,

Implementors§