diff --git a/node/libs/concurrency/src/error.rs b/node/libs/concurrency/src/error.rs new file mode 100644 index 00000000..18621112 --- /dev/null +++ b/node/libs/concurrency/src/error.rs @@ -0,0 +1,19 @@ +//! Generalization of anyhow::Context to more structured errors. +use std::fmt::Display; + +/// Trait complementary to anyhow::Context which allows for +/// adding context to error types which contain anyhow::Error. +pub trait Wrap: Sized { + /// Appends context `c` to the error. + fn wrap(self, c: C) -> Self { + self.with_wrap(|| c) + } + /// Appends context `f()` to the error. + fn with_wrap C>(self, f: F) -> Self; +} + +impl Wrap for Result { + fn with_wrap C>(self, f: F) -> Self { + self.map_err(|err| err.with_wrap(f)) + } +}