pub trait ErrorWrap<T, E>: Sealedwhere
E: Send + Sync + 'static,{
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
sourcefn wrap<C>(self, context: C) -> Result<T, Error>where
C: Display + Send + Sync + 'static,
fn wrap<C>(self, context: C) -> Result<T, Error>where
C: Display + Send + Sync + 'static,
Wrap an error value with additional context.
sourcefn wrap_with<C, F>(self, f: F) -> Result<T, Error>where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
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.