From 02cb3e74b6780ee6f68de4e71b9647a61872ef6b Mon Sep 17 00:00:00 2001 From: Andrey Tkachenko Date: Fri, 12 May 2023 12:37:53 +0400 Subject: [PATCH] better message display --- Cargo.toml | 4 ++-- derive/Cargo.toml | 2 +- derive/src/lib.rs | 23 ++++++++++------------- examples/demo.rs | 6 ++++-- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4753689..dcae561 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ctxerr" -version = "0.2.6" +version = "0.2.7" authors = ["Andrey Tkachenko "] repository = "https://github.com/andreytkachenko/ctxerr.git" keywords = ["error", "handling", "thiserror", "context", "backtrace"] @@ -15,5 +15,5 @@ resolver = "2" members = ["derive"] [dependencies] -ctxerr_derive = {version = "0.5.2", path = "derive"} +ctxerr_derive = {version = "0.5.3", path = "derive"} thiserror = "1.0.40" diff --git a/derive/Cargo.toml b/derive/Cargo.toml index 6907e32..0151996 100644 --- a/derive/Cargo.toml +++ b/derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ctxerr_derive" -version = "0.5.2" +version = "0.5.3" authors = ["Andrey Tkachenko "] repository = "https://github.com/andreytkachenko/ctxerr.git" keywords = ["error", "handling", "thiserror", "context", "backtrace"] diff --git a/derive/src/lib.rs b/derive/src/lib.rs index 1a20554..01eb4a3 100644 --- a/derive/src/lib.rs +++ b/derive/src/lib.rs @@ -46,26 +46,22 @@ pub fn ctxerr( pub location: Option<&'static core::panic::Location<'static>>, } - impl std::fmt::Debug for #base_name { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - writeln!(f, "{:?}: {}", self.kind, self.kind)?; - if let Some(b) = &self.backtrace { - writeln!(f, "backtrace: \n{}", b)?; - } - - Ok(()) - } - } - impl std::fmt::Display for #base_name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{:?}: {}", self.kind, self.kind)?; + write!(f, "{}", self.kind)?; if let Some(loc) = self.location { - writeln!(f, " file: {}:{}:{}", loc.file(), loc.line(), loc.column())?; + writeln!(f, " in file: {}:{}:{}", loc.file(), loc.line(), loc.column())?; } else { writeln!(f)?; } + Ok(()) + } + } + + impl std::fmt::Debug for #base_name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", &self)?; if let Some(b) = &self.backtrace { writeln!(f, "backtrace: \n{}", b)?; } @@ -74,6 +70,7 @@ pub fn ctxerr( } } + impl From for #base_name { #[track_caller] fn from(kind: ErrorKind) -> Self { diff --git a/examples/demo.rs b/examples/demo.rs index 7d90308..be0c1af 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -2,7 +2,7 @@ use ctxerr_derive::ctxerr; #[ctxerr] pub enum ErrorKind { - #[error("Some error {0}")] + #[error("Error: {0}")] Error1(#[from] std::io::Error), } @@ -12,11 +12,13 @@ fn operation() -> Result<(), std::io::Error> { } fn test() -> Result<(), Error> { - Ok(operation()?) + Ok(operation().map_err(ErrorKind::from)?) } fn main() { if let Err(err) = test() { println!("{}", err); + + println!("{:?}", err); } }