diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:06:33 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:06:33 +0100 |
| commit | dc0ee6ce99a0480b7a6c228492936b16ceaf60cd (patch) | |
| tree | 7ab8e10bf2fce88ca3cdfb2a6b13c0717c638407 | |
| parent | 1b50c5ce06d2356124991fa48e6881f61f646ca3 (diff) | |
✨feat: add a more spesific error for the download process
| -rw-r--r-- | src/downloader.rs | 14 | ||||
| -rw-r--r-- | src/error.rs | 20 |
2 files changed, 31 insertions, 3 deletions
diff --git a/src/downloader.rs b/src/downloader.rs index 64df06e..0141424 100644 --- a/src/downloader.rs +++ b/src/downloader.rs @@ -2,7 +2,7 @@ use futures_util::StreamExt; use std::path::PathBuf; use tokio::{fs, io::AsyncWriteExt}; -use crate::{Result, appimages_dir, make_progress_bar}; +use crate::{Error, Result, appimages_dir, make_progress_bar}; #[derive(Debug, Default)] pub struct Downloader {} @@ -23,7 +23,12 @@ impl Downloader { pub async fn download_with_progress(&self, url: &str, path: &PathBuf) -> Result<()> { fs::create_dir_all(&appimages_dir()?).await?; - let resp = reqwest::get(&url.to_string()).await?; + let resp = reqwest::get(&url.to_string()) + .await + .map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let total_size = resp.content_length().unwrap_or(0); let bar = make_progress_bar(total_size)?; @@ -32,7 +37,10 @@ impl Downloader { // Stream download with progress updates let mut stream = resp.bytes_stream(); while let Some(chunk) = stream.next().await { - let chunk = chunk?; + let chunk = chunk.map_err(|source| Error::Download { + url: url.to_string(), + source, + })?; let len = chunk.len() as u64; out.write_all(&chunk).await?; bar.inc(len); diff --git a/src/error.rs b/src/error.rs index 39c421d..b473469 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,10 @@ pub type Result<T> = core::result::Result<T, Error>; pub enum Error { InvalidPath, NotFound(String), + Download { + url: String, + source: reqwest::Error, + }, #[from] Io(std::io::Error), @@ -36,6 +40,22 @@ impl core::fmt::Display for Error { Error::EnvVar(e) => write!(fmt, "Environment variable error: {e}"), Error::InvalidPath => write!(fmt, "Invalid path provided"), Error::IndicatifTemplate(e) => write!(fmt, "Progress bar template error: {e}"), + Error::Download { url, source } => { + if source.is_timeout() { + write!(fmt, "Download timed out from: {url}") + } else if source.is_connect() { + write!(fmt, "Failed to connect to: {url}") + } else if let Some(status) = source.status() { + match status.as_u16() { + 404 => write!(fmt, "AppImage not found at: {url}"), + 403 => write!(fmt, "Access denied at: {url}"), + 500..=599 => write!(fmt, "Server error when downloading from: {url}"), + _ => write!(fmt, "HTTP {status} error downloading from: {url}"), + } + } else { + write!(fmt, "Failed to download from {url}: {source}") + } + } } } } |
