diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:08:45 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-08-03 10:08:45 +0100 |
| commit | 57ffe6ff51a8f3ccc41607f18dcf3c9039e85d94 (patch) | |
| tree | 7ab8e10bf2fce88ca3cdfb2a6b13c0717c638407 /src/error.rs | |
| parent | 6b3f5d37fbce2e880a454e66d8fbd78269f1c867 (diff) | |
| parent | dc0ee6ce99a0480b7a6c228492936b16ceaf60cd (diff) | |
Merge pull request 'Add comprehensive error types' (#12) from feat/issue-5 into main
Reviewed-on: https://git.ch-naseem.com/ndpm13/zap-rs/pulls/12
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..b473469 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,63 @@ +use derive_more::From; + +pub type Result<T> = core::result::Result<T, Error>; + +#[derive(Debug, From)] +pub enum Error { + InvalidPath, + NotFound(String), + Download { + url: String, + source: reqwest::Error, + }, + + #[from] + Io(std::io::Error), + + #[from] + Json(serde_json::Error), + + #[from] + Http(reqwest::Error), + + #[from] + EnvVar(std::env::VarError), + + #[from] + IndicatifTemplate(indicatif::style::TemplateError), +} + +impl core::fmt::Display for Error { + fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), std::fmt::Error> { + match self { + Error::Io(e) => match e.kind() { + std::io::ErrorKind::NotFound => write!(fmt, "File or directory not found"), + _ => write!(fmt, "IO error: {e}"), + }, + Error::NotFound(name) => write!(fmt, "Application '{name}' not found"), + Error::Json(e) => write!(fmt, "JSON error: {e}"), + Error::Http(e) => write!(fmt, "HTTP error: {e}"), + 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}") + } + } + } + } +} + +impl std::error::Error for Error {} |
