Trait pingora_core::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.

Object Safety§

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§