From c19ea3979aa55e37695f46a43c382547739f020a Mon Sep 17 00:00:00 2001 From: Alexander WB Date: Tue, 25 Feb 2025 03:27:59 +0100 Subject: [PATCH] :ok_hand: Improved error handling --- crates/librqbit/src/blocklist.rs | 19 +++++++------------ crates/librqbit/src/session.rs | 1 + 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/crates/librqbit/src/blocklist.rs b/crates/librqbit/src/blocklist.rs index fe90e0a..f67f1d4 100644 --- a/crates/librqbit/src/blocklist.rs +++ b/crates/librqbit/src/blocklist.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use async_compression::tokio::bufread::GzipDecoder; use futures::TryStreamExt; use intervaltree::IntervalTree; @@ -28,12 +28,11 @@ impl Blocklist { } pub async fn load_from_url(url: &str) -> Result { - let response = reqwest::get(url).await.map_err(|e| anyhow::anyhow!(e))?; + let response = reqwest::get(url) + .await + .context("Failed to send request for blocklist")?; if response.status() != 200 { - return Err(anyhow::anyhow!( - "Failed to fetch blocklist: HTTP {}", - response.status() - )); + anyhow::bail!("Failed to fetch blocklist: HTTP {}", response.status()); } let content_length = response @@ -41,9 +40,7 @@ impl Blocklist { .ok_or_else(|| anyhow::anyhow!("Failed to get content length"))?; if content_length < 2 { - return Err(anyhow::anyhow!( - "Content too short: not enough data to determine compression" - )); + anyhow::bail!("Content too short: not enough data to determine compression"); } let reader = StreamReader::new( @@ -72,9 +69,7 @@ impl Blocklist { if buffer.len() >= 2 { peek_bytes.copy_from_slice(&buffer[0..2]); } else { - return Err(anyhow::anyhow!( - "Content too short: not enough data to determine compression" - )); + anyhow::bail!("Content too short: not enough data to determine compression"); } // Check for Gzip magic bytes (1F 8B) diff --git a/crates/librqbit/src/session.rs b/crates/librqbit/src/session.rs index 376c419..c68de6f 100644 --- a/crates/librqbit/src/session.rs +++ b/crates/librqbit/src/session.rs @@ -615,6 +615,7 @@ impl Session { let blocklist: blocklist::Blocklist = if let Some(blocklist_url) = opts.blocklist_url { blocklist::Blocklist::load_from_url(&blocklist_url) .await + .inspect_err(|e| warn!("failed to read blocklist: {e}")) .unwrap() } else { blocklist::Blocklist::empty()