From e522bf45f2cb1986207e79655656a983b00aad0d Mon Sep 17 00:00:00 2001 From: monosans Date: Tue, 30 Sep 2025 07:53:39 +0300 Subject: [PATCH] fix: improve error messages --- src/config.rs | 2 +- src/ipdb.rs | 12 ++++++------ src/output.rs | 15 +++++---------- src/raw_config.rs | 4 ++-- src/scraper.rs | 19 ++++++++++++++----- 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/config.rs b/src/config.rs index c0657dc..796c116 100644 --- a/src/config.rs +++ b/src/config.rs @@ -78,7 +78,7 @@ async fn get_output_path( raw_config.output.path.clone() }; tokio::fs::create_dir_all(&output_path).await.wrap_err_with(|| { - format!("failed to create output directory: {}", output_path.display()) + format!("failed to create directory: {}", output_path.display()) })?; Ok(output_path) } diff --git a/src/ipdb.rs b/src/ipdb.rs index 7949c20..997f49e 100644 --- a/src/ipdb.rs +++ b/src/ipdb.rs @@ -61,11 +61,11 @@ impl DbType { let db_path = self.db_path().await?; let mut file = tokio::fs::File::create(&db_path).await.wrap_err_with(|| { - format!("failed to create file {}", db_path.display()) + format!("failed to create file: {}", db_path.display()) })?; while let Some(chunk) = response.chunk().await? { file.write_all(&chunk).await.wrap_err_with(|| { - format!("failed to write to file {}", db_path.display()) + format!("failed to write to file: {}", db_path.display()) })?; #[cfg(feature = "tui")] drop( @@ -81,7 +81,7 @@ impl DbType { async fn save_etag(self, etag: impl AsRef<[u8]>) -> crate::Result<()> { let path = self.etag_path().await?; tokio::fs::write(&path, etag).await.wrap_err_with(move || { - format!("failed to write to file {}", path.display()) + format!("failed to write to file: {}", path.display()) }) } @@ -93,7 +93,7 @@ impl DbType { Ok(text) => Ok(text.parse().ok()), Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None), Err(e) => Err(e).wrap_err_with(move || { - format!("failed to read file {} to string", path.display()) + format!("failed to read file to string: {}", path.display()) }), } } @@ -104,7 +104,7 @@ impl DbType { Ok(()) => Ok(()), Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()), Err(e) => Err(e).wrap_err_with(move || { - format!("failed to remove {}", path.display()) + format!("failed to remove file: {}", path.display()) }), } } @@ -185,7 +185,7 @@ impl DbType { tokio::task::spawn_blocking(move || maxminddb::Reader::open_mmap(path)) .await? .wrap_err_with(move || { - format!("failed to open {} database", self.name()) + format!("failed to open IP database: {}", self.name()) }) } } diff --git a/src/output.rs b/src/output.rs index 9b8453c..8285742 100644 --- a/src/output.rs +++ b/src/output.rs @@ -138,7 +138,7 @@ pub async fn save_proxies( Ok(()) => Ok(()), Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()), Err(e) => Err(e).wrap_err_with(|| { - format!("failed to remove file {}", path.display()) + format!("failed to remove file: {}", path.display()) }), }?; let json_data = if pretty { @@ -147,9 +147,7 @@ pub async fn save_proxies( serde_json::to_vec(&proxy_dicts)? }; tokio::fs::write(&path, json_data).await.wrap_err_with( - move || { - format!("failed to write proxies to {}", path.display()) - }, + move || format!("failed to write to file: {}", path.display()), )?; } } @@ -162,7 +160,7 @@ pub async fn save_proxies( Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()), Err(e) => Err(e).wrap_err_with(|| { format!( - "failed to remove directory {}", + "failed to remove directory: {}", directory_path.display() ) }), @@ -181,7 +179,7 @@ pub async fn save_proxies( .await .wrap_err_with(|| { format!( - "failed to write proxies to {}", + "failed to write to file: {}", directory_path.join("all.txt").display() ) })?; @@ -192,10 +190,7 @@ pub async fn save_proxies( file_path.set_extension("txt"); tokio::fs::write(&file_path, text).await.wrap_err_with( move || { - format!( - "failed to write proxies to {}", - file_path.display() - ) + format!("failed to write to file: {}", file_path.display()) }, )?; } diff --git a/src/raw_config.rs b/src/raw_config.rs index 557eaf8..60d0567 100644 --- a/src/raw_config.rs +++ b/src/raw_config.rs @@ -182,9 +182,9 @@ pub fn get_config_path() -> String { pub async fn read_config(path: &Path) -> crate::Result { let raw_config = tokio::fs::read_to_string(path).await.wrap_err_with(move || { - format!("failed to read {} to string", path.display()) + format!("failed to read file to string: {}", path.display()) })?; toml::from_str(&raw_config).wrap_err_with(move || { - format!("failed to parse {} as TOML config file", path.display()) + format!("failed to parse TOML config file: {}", path.display()) }) } diff --git a/src/scraper.rs b/src/scraper.rs index ee524d5..97cfb16 100644 --- a/src/scraper.rs +++ b/src/scraper.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use color_eyre::eyre::OptionExt as _; +use color_eyre::eyre::{OptionExt as _, WrapErr as _}; use foldhash::HashSetExt as _; #[cfg(feature = "tui")] @@ -46,15 +46,24 @@ async fn scrape_one( _ => { drop(http_client); match u.to_file_path() { - Ok(path) => tokio::fs::read_to_string(path).await, - Err(()) => tokio::fs::read_to_string(&source.url).await, + Ok(path) => tokio::fs::read_to_string(path) + .await + .wrap_err_with(move || { + format!("failed to read file to string: {u}") + }), + Err(()) => tokio::fs::read_to_string(&source.url) + .await + .wrap_err_with(move || { + format!("failed to read file to string: {u}") + }), } - .map_err(Into::into) } } } else { drop(http_client); - tokio::fs::read_to_string(&source.url).await.map_err(Into::into) + tokio::fs::read_to_string(&source.url).await.wrap_err_with(|| { + format!("failed to read file to string: {}", source.url) + }) }; #[cfg(feature = "tui")]