diff --git a/Cargo.lock b/Cargo.lock index d7ccc059..c18ece69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -947,6 +947,7 @@ dependencies = [ "tracing-opentelemetry", "tracing-subscriber", "trust-dns-resolver", + "wee-woo", "xdg", ] @@ -3871,6 +3872,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wee-woo" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "005e18f8367a2e0e375c29dd4d266c97e7590ec9d64bb05dc5af054a555eda05" + [[package]] name = "weezl" version = "0.1.8" diff --git a/Cargo.toml b/Cargo.toml index c9754bb9..82ee8554 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -144,6 +144,7 @@ tracing-flame = "0.2.0" tracing-opentelemetry = "0.25.0" tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json"] } trust-dns-resolver = "0.23.2" +wee-woo = "0.1.0" xdg = "2.5.2" [target.'cfg(unix)'.dependencies] diff --git a/src/error.rs b/src/error.rs index 49cf7307..ed8c2208 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,39 +1,11 @@ //! Error handling facilities -use std::{fmt, iter, path::PathBuf}; +use std::path::PathBuf; use thiserror::Error; use crate::config::ListenConfig; -/// Formats an [`Error`][0] and its [`source`][1]s with a separator -/// -/// [0]: std::error::Error -/// [1]: std::error::Error::source -pub(crate) struct DisplayWithSources<'a> { - /// The error (and its sources) to write - pub(crate) error: &'a dyn std::error::Error, - - /// Separator to write between the original error and subsequent sources - pub(crate) infix: &'static str, -} - -impl fmt::Display for DisplayWithSources<'_> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.error)?; - - let mut source = self.error.source(); - - source - .into_iter() - .chain(iter::from_fn(|| { - source = source.and_then(std::error::Error::source); - source - })) - .try_for_each(|source| write!(f, "{}{source}", self.infix)) - } -} - /// Top-level errors // Missing docs are allowed here since that kind of information should be // encoded in the error messages themselves anyway. diff --git a/src/main.rs b/src/main.rs index 8f4268a1..a0930e73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use clap::Parser; #[cfg(all(not(target_env = "msvc"), feature = "jemalloc"))] use tikv_jemallocator::Jemalloc; use tracing::{error, info}; +use wee_woo::ErrorExt; mod api; mod cli; @@ -100,13 +101,7 @@ async fn main() -> ExitCode { return ExitCode::SUCCESS; }; - eprintln!( - "Error: {}", - error::DisplayWithSources { - error: &e, - infix: "\n Caused by: " - } - ); + eprintln!("Error: {}", e.display_with_sources("\n Caused by: ")); ExitCode::FAILURE }