Trait narrate::ErrorWrap

source ·
pub trait ErrorWrap<T, E>: Sealedwhere
    E: Send + Sync + 'static,{
    // Required methods
    fn wrap<C>(self, context: C) -> Result<T, Error>
       where C: Display + Send + Sync + 'static;
    fn wrap_with<C, F>(self, f: F) -> Result<T, Error>
       where C: Display + Send + Sync + 'static,
             F: FnOnce() -> C;
    fn add_help(self, help: &'static str) -> Result<T, Error>;
    fn add_help_with<C, F>(self, f: F) -> Result<T, Error>
       where C: Display + Send + Sync + 'static,
             F: FnOnce() -> C;
}
Expand description

Provides wrap and add_help methods for Result.

This trait is sealed and cannot be implemented for types outside of narrate.

Useful for wrapping a potential error with additional context and/or help message.

Lazy evaluation

Use wrap_with and add_help_with methods for lazily evaluation of the added context.

Example

use narrate::{ErrorWrap, Result};
use std::fs;
use std::path::PathBuf;

pub struct ImportantThing {
    path: PathBuf,
}

impl ImportantThing {
    pub fn detach(&mut self) -> Result<()> {...}
}

pub fn do_it(mut it: ImportantThing) -> Result<Vec<u8>> {
    it.detach().wrap("Failed to detach the important thing")?;

    let path = &it.path;
    let content = fs::read(path)
        .wrap_with(|| format!("Failed to read instrs from {}", path.display()))
        .add_help("list of instr in README.md")?;

    Ok(content)
}

Required Methods§

source

fn wrap<C>(self, context: C) -> Result<T, Error>where C: Display + Send + Sync + 'static,

Wrap an error value with additional context.

source

fn wrap_with<C, F>(self, f: F) -> Result<T, Error>where C: Display + Send + Sync + 'static, F: FnOnce() -> C,

Wrap an error value with lazily evaluated context.

source

fn add_help(self, help: &'static str) -> Result<T, Error>

Add a help message to an error value.

source

fn add_help_with<C, F>(self, f: F) -> Result<T, Error>where C: Display + Send + Sync + 'static, F: FnOnce() -> C,

Add a lazily evaluated help message to an error value.

Implementations on Foreign Types§

source§

impl<T, E> ErrorWrap<T, E> for Result<T, E>where E: StdError + Send + Sync + 'static,

source§

fn wrap<C>(self, context: C) -> Result<T, Error>where C: Display + Send + Sync + 'static,

source§

fn wrap_with<C, F>(self, f: F) -> Result<T, Error>where C: Display + Send + Sync + 'static, F: FnOnce() -> C,

source§

fn add_help(self, help: &'static str) -> Result<T, Error>

source§

fn add_help_with<C, F>(self, f: F) -> Result<T, Error>where C: Display + Send + Sync + 'static, F: FnOnce() -> C,

Implementors§