ErrWith

Trait ErrWith 

Source
pub trait ErrWith<ReportErr, ReportOk, E> {
    // Required methods
    fn err_with<F>(self, f: F) -> Result<ReportOk, (ReportErr, E)>
       where F: FnOnce() -> ReportErr;
    fn err_with_report(
        self,
        report: &ReportErr,
    ) -> Result<ReportOk, (ReportErr, E)>
       where ReportErr: Clone;
}
Expand description

This trait allows adding extra context or information to an error, creating a tuple of the additional context and the original error. This is particularly useful for error handling when you want to include more details in the error without losing the original error value.

The ErrWith trait provides methods to wrap an error with additional context, either by using a closure that generates the context or by directly providing the context.

Required Methods§

Source

fn err_with<F>(self, f: F) -> Result<ReportOk, (ReportErr, E)>
where F: FnOnce() -> ReportErr,

Takes a closure f that returns a value of type ReportErr, and uses it to wrap an error of type (ReportErr, E) in the context of a Result of type ReportOk.

This method allows you to add additional context to an error by providing a closure that generates the context.

§Arguments
  • f - A closure that returns the additional context of type ReportErr.
§Returns

A Result of type ReportOk if the original result is Ok, or a tuple (ReportErr, E) containing the additional context and the original error if the original result is Err.

§Errors

qqq: errors

§Example
use error_tools::ErrWith;
let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
let result_with_context : Result< (), ( &str, std::io::Error ) > = result.err_with( || "additional context" );
Source

fn err_with_report(self, report: &ReportErr) -> Result<ReportOk, (ReportErr, E)>
where ReportErr: Clone,

Takes a reference to a ReportErr value and uses it to wrap an error of type (ReportErr, E) in the context of a Result of type ReportOk.

This method allows you to add additional context to an error by providing a reference to the context.

§Arguments
  • report - A reference to the additional context of type ReportErr.
§Returns

A Result of type ReportOk if the original result is Ok, or a tuple (ReportErr, E) containing the additional context and the original error if the original result is Err.

§Errors

qqq: Errors

§Example
use error_tools::ErrWith;
let result : Result< (), std::io::Error > = Err( std::io::Error::new( std::io::ErrorKind::Other, "an error occurred" ) );
let report = "additional context";
let result_with_report : Result< (), ( &str, std::io::Error ) > = result.err_with_report( &report );

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<ReportErr, ReportOk, E, IntoError> ErrWith<ReportErr, ReportOk, E> for Result<ReportOk, IntoError>
where IntoError: Into<E>,

Source§

fn err_with<F>(self, f: F) -> Result<ReportOk, (ReportErr, E)>
where F: FnOnce() -> ReportErr,

Source§

fn err_with_report(self, report: &ReportErr) -> Result<ReportOk, (ReportErr, E)>
where ReportErr: Clone, Result<ReportOk, IntoError>: Sized,

Implementors§