summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2019-04-01 16:24:29 +0200
committerLinus Färnstrand <linus@mullvad.net>2019-04-01 23:19:33 +0200
commit1d7a78c8c802dae413d43d3cd88352724a15ff9a (patch)
treed5fe20b62df225b74ee9a4a7f11a323e29bd0437
parent5a6a7131c5ea786aca0831475763d9a2843fac45 (diff)
downloadmullvadvpn-1d7a78c8c802dae413d43d3cd88352724a15ff9a.tar.xz
mullvadvpn-1d7a78c8c802dae413d43d3cd88352724a15ff9a.zip
Add ErrorExt::display_chain_with_msg
-rw-r--r--talpid-core/src/lib.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs
index 1147e40fd6..8de9002c1a 100644
--- a/talpid-core/src/lib.rs
+++ b/talpid-core/src/lib.rs
@@ -62,7 +62,11 @@ mod linux;
trait ErrorExt {
+ /// Creates a string representation of the entire error chain.
fn display_chain(&self) -> String;
+
+ /// Like [`display_chain`] but with an extra message at the start of the chain
+ fn display_chain_with_msg(&self, msg: &str) -> String;
}
impl<E: std::error::Error> ErrorExt for E {
@@ -75,4 +79,14 @@ impl<E: std::error::Error> ErrorExt for E {
}
s
}
+
+ fn display_chain_with_msg(&self, msg: &str) -> String {
+ let mut s = format!("Error: {}\nCaused by: {}", msg, self);
+ let mut source = self.source();
+ while let Some(error) = source {
+ s.push_str(&format!("\nCaused by: {}", error));
+ source = error.source();
+ }
+ s
+ }
}