pingora_core::prelude

Trait OrErr

Source
pub trait OrErr<T, E> {
    // Required methods
    fn or_err(
        self,
        et: ErrorType,
        context: &'static str,
    ) -> Result<T, Box<Error>>
       where E: Into<Box<dyn Error + Sync + Send>>;
    fn or_err_with<C, F>(
        self,
        et: ErrorType,
        context: F,
    ) -> Result<T, Box<Error>>
       where C: Into<ImmutStr>,
             F: FnOnce() -> C,
             E: Into<Box<dyn Error + Sync + Send>>;
    fn explain_err<C, F>(
        self,
        et: ErrorType,
        context: F,
    ) -> Result<T, Box<Error>>
       where C: Into<ImmutStr>,
             F: FnOnce(E) -> C;
    fn or_fail(self) -> Result<T, Box<Error>>
       where E: Into<Box<dyn Error + Sync + Send>>;
}
Expand description

Helper trait to chain errors with context

Required Methods§

Source

fn or_err(self, et: ErrorType, context: &'static str) -> Result<T, Box<Error>>
where E: Into<Box<dyn Error + Sync + Send>>,

Wrap the E in Result with new ErrorType and context, the existing E will be the cause.

This is a shortcut for map_err() + because()

Source

fn or_err_with<C, F>(self, et: ErrorType, context: F) -> Result<T, Box<Error>>
where C: Into<ImmutStr>, F: FnOnce() -> C, E: Into<Box<dyn Error + Sync + Send>>,

Similar to or_err(), but takes a closure, which is useful for constructing String.

Source

fn explain_err<C, F>(self, et: ErrorType, context: F) -> Result<T, Box<Error>>
where C: Into<ImmutStr>, F: FnOnce(E) -> C,

Replace the E in Result with a new Error generated from the current error

This is useful when the current error cannot move out of scope. This is a shortcut for map_err() + explain().

Source

fn or_fail(self) -> Result<T, Box<Error>>
where E: Into<Box<dyn Error + Sync + Send>>,

Similar to or_err() but just to surface errors that are not Error (where ? cannot be used directly).

or_err()/or_err_with() are still preferred because they make the error more readable and traceable.

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, E> OrErr<T, E> for Result<T, E>

Source§

fn or_err(self, et: ErrorType, context: &'static str) -> Result<T, Box<Error>>
where E: Into<Box<dyn Error + Sync + Send>>,

Source§

fn or_err_with<C, F>(self, et: ErrorType, context: F) -> Result<T, Box<Error>>
where C: Into<ImmutStr>, F: FnOnce() -> C, E: Into<Box<dyn Error + Sync + Send>>,

Source§

fn explain_err<C, F>(self, et: ErrorType, exp: F) -> Result<T, Box<Error>>
where C: Into<ImmutStr>, F: FnOnce(E) -> C,

Source§

fn or_fail(self) -> Result<T, Box<Error>>
where E: Into<Box<dyn Error + Sync + Send>>,

Implementors§