pingora_error

Trait OrErr

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

Helper trait to chain errors with context

Required Methods§

Source

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

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: Into<ImmutStr>, F: FnOnce() -> C>( self, et: ErrorType, context: F, ) -> Result<T, BError>
where E: Into<Box<dyn ErrorTrait + Send + Sync>>,

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

Source

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

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>
where E: Into<Box<dyn ErrorTrait + Send + Sync>>,

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.

Implementors§

Source§

impl<T, E> OrErr<T, E> for Result<T, E>