better message display

This commit is contained in:
Andrey Tkachenko 2023-05-12 12:37:53 +04:00
parent 6fa9a0d37d
commit 02cb3e74b6
4 changed files with 17 additions and 18 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ctxerr" name = "ctxerr"
version = "0.2.6" version = "0.2.7"
authors = ["Andrey Tkachenko <andrey@aidev.ru>"] authors = ["Andrey Tkachenko <andrey@aidev.ru>"]
repository = "https://github.com/andreytkachenko/ctxerr.git" repository = "https://github.com/andreytkachenko/ctxerr.git"
keywords = ["error", "handling", "thiserror", "context", "backtrace"] keywords = ["error", "handling", "thiserror", "context", "backtrace"]
@ -15,5 +15,5 @@ resolver = "2"
members = ["derive"] members = ["derive"]
[dependencies] [dependencies]
ctxerr_derive = {version = "0.5.2", path = "derive"} ctxerr_derive = {version = "0.5.3", path = "derive"}
thiserror = "1.0.40" thiserror = "1.0.40"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ctxerr_derive" name = "ctxerr_derive"
version = "0.5.2" version = "0.5.3"
authors = ["Andrey Tkachenko <andrey@aidev.ru>"] authors = ["Andrey Tkachenko <andrey@aidev.ru>"]
repository = "https://github.com/andreytkachenko/ctxerr.git" repository = "https://github.com/andreytkachenko/ctxerr.git"
keywords = ["error", "handling", "thiserror", "context", "backtrace"] keywords = ["error", "handling", "thiserror", "context", "backtrace"]

View File

@ -46,26 +46,22 @@ pub fn ctxerr(
pub location: Option<&'static core::panic::Location<'static>>, 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 { impl std::fmt::Display for #base_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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 { 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 { } else {
writeln!(f)?; 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 { if let Some(b) = &self.backtrace {
writeln!(f, "backtrace: \n{}", b)?; writeln!(f, "backtrace: \n{}", b)?;
} }
@ -74,6 +70,7 @@ pub fn ctxerr(
} }
} }
impl From<ErrorKind> for #base_name { impl From<ErrorKind> for #base_name {
#[track_caller] #[track_caller]
fn from(kind: ErrorKind) -> Self { fn from(kind: ErrorKind) -> Self {

View File

@ -2,7 +2,7 @@ use ctxerr_derive::ctxerr;
#[ctxerr] #[ctxerr]
pub enum ErrorKind { pub enum ErrorKind {
#[error("Some error {0}")] #[error("Error: {0}")]
Error1(#[from] std::io::Error), Error1(#[from] std::io::Error),
} }
@ -12,11 +12,13 @@ fn operation() -> Result<(), std::io::Error> {
} }
fn test() -> Result<(), Error> { fn test() -> Result<(), Error> {
Ok(operation()?) Ok(operation().map_err(ErrorKind::from)?)
} }
fn main() { fn main() {
if let Err(err) = test() { if let Err(err) = test() {
println!("{}", err); println!("{}", err);
println!("{:?}", err);
} }
} }