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_no_source(self) -> Result<T, Bug>;
    fn bug(self) -> Result<T, Bug>
       where S: StdError + 'static;
    fn bug_no_source_force(self) -> Result<T, Bug>;
    fn bug_force(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_no_source(self) -> Result<T, Bug>

Convert any Result::Err into a Result::Err wrapping a Bug Use bug instead if the error implements std::error::Error

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

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

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

Convert any Result::Err wrapping an error that implements std::error::Error into a Result::Err wrapping a Bug

fn foo() -> Result<(), Error> {
   Err(sqlx::Error::RowNotFound)
       .bug()
       .with_context("Configuration file foo.conf is missing.")?;
}
Source

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

Convert any Result::Err into a Result::Err wrapping a Bug forcing backtrace capture Use bug_force instead if the error implements std::error::Error

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(self) -> Result<T, Bug>
where S: StdError + 'static,

Convert any Result::Err wrapping an error that implements std::error::Error into a Result::Err wrapping a Bug forcing backtrace capture

fn foo() -> Result<(), Error> {
   Err(sqlx::Error::RowNotFound)
       .bug_force()
       .with_context("Configuration file foo.conf is missing.")?;
}

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_no_source(self) -> Result<T, Bug>

Source§

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

Source§

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

Source§

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

Implementors§