From e8fb9703b2aae4658364419d97c7f09a69fc56fe Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Fri, 25 Nov 2022 22:06:13 +0800 Subject: [PATCH] feat: Improve display for error Signed-off-by: Xuanwo --- src/error.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 92be9101898..69bcfcf8f47 100644 --- a/src/error.rs +++ b/src/error.rs @@ -142,12 +142,30 @@ impl Display for Error { write!(f, " }}")?; } - write!(f, " => {}", self.message) + write!(f, " => {}", self.message)?; + + if let Some(source) = &self.source { + write!(f, ", source: {}", source)?; + } + + Ok(()) } } impl Debug for Error { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + // If alternate has been specified, we will print like Debug. + if f.alternate() { + let mut de = f.debug_struct("Error"); + de.field("kind", &self.kind); + de.field("message", &self.message); + de.field("status", &self.status); + de.field("operation", &self.operation); + de.field("context", &self.context); + de.field("source", &self.source); + return de.finish(); + } + writeln!( f, "{} ({}) at {} => {}", @@ -266,3 +284,47 @@ impl From for io::Error { io::Error::new(kind, err) } } + +#[cfg(test)] +mod tests { + use super::*; + use anyhow::anyhow; + use once_cell::sync::Lazy; + + static TEST_ERROR: Lazy = Lazy::new(|| Error { + kind: ErrorKind::Unexpected, + message: "something wrong happened".to_string(), + status: ErrorStatus::Permanent, + operation: "Read", + context: vec![ + ("path", "/path/to/file".to_string()), + ("called", "send_async".to_string()), + ], + source: Some(anyhow!("networking error")), + }); + + #[test] + fn test_error_display() { + let s = format!("{}", Lazy::force(&TEST_ERROR)); + assert_eq!( + s, + r#"Unexpected (permanent) at Read, context: { path: /path/to/file, called: send_async } => something wrong happened, source: networking error"# + ) + } + + #[test] + fn test_error_debug() { + let s = format!("{:?}", Lazy::force(&TEST_ERROR)); + assert_eq!( + s, + r#"Unexpected (permanent) at Read => something wrong happened + +Context: + path: /path/to/file + called: send_async + +Source: networking error +"# + ) + } +}