From be40383b1a225c0cf08e78093bf07e315f5a92ba Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Sun, 16 Jul 2023 15:17:01 +0200 Subject: [PATCH] impl from reqwest error --- article_scraper/src/images/error.rs | 6 ++++++ article_scraper/src/images/request.rs | 8 ++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/article_scraper/src/images/error.rs b/article_scraper/src/images/error.rs index 7135f56..6c87710 100644 --- a/article_scraper/src/images/error.rs +++ b/article_scraper/src/images/error.rs @@ -23,3 +23,9 @@ pub enum ImageDownloadError { #[error("Unknown Error")] Unknown, } + +impl From for ImageDownloadError { + fn from(_value: reqwest::Error) -> Self { + Self::Http + } +} diff --git a/article_scraper/src/images/request.rs b/article_scraper/src/images/request.rs index ab64f41..c145598 100644 --- a/article_scraper/src/images/request.rs +++ b/article_scraper/src/images/request.rs @@ -14,11 +14,7 @@ pub struct ImageRequest { impl ImageRequest { pub async fn new(url: String, client: &Client) -> Result { - let response = client - .get(&url) - .send() - .await - .map_err(|_| ImageDownloadError::Http)?; + let response = client.get(&url).send().await?; let content_type = Self::get_content_type(&response)?; let content_length = Self::get_content_length(&response)?; @@ -40,7 +36,7 @@ impl ImageRequest { let mut result = Vec::with_capacity(self.content_length); while let Some(item) = stream.next().await { - let chunk = item.map_err(|_| ImageDownloadError::Http)?; + let chunk = item?; _ = tx.send(chunk.len()).await; for byte in chunk { result.push(byte);