Skip to content

Commit

Permalink
fix: fix WithContext to prevent old error msg from being overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
imlk0 authored and oxr463 committed Jul 28, 2021
1 parent 644457f commit 7da26dd
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ where
C: Display + Send + Sync + 'static,
F: FnOnce() -> C,
{
self.map_err(|error| Into::<Error>::into(error).with_msg(f()))
match self {
Ok(t) => Ok(t),
Err(e) => Err(e).context(f()),
}
}
}

#[allow(dead_code)]
impl<T> WithContext<T> for result::Result<T, Error> {
fn context<C>(self, context: C) -> Result<T>
where
C: Display + Send + Sync + 'static,
{
self.map_err(|error| {
if let Some(ref msg) = error.msg {
let new_msg = format!("{}: {}", context, msg);
error.with_msg(new_msg)
} else {
error.with_msg(context)
}
})
}
}

0 comments on commit 7da26dd

Please sign in to comment.