zap_rs/
error.rs

1use derive_more::From;
2
3pub type Result<T> = core::result::Result<T, Error>;
4
5#[derive(Debug, From)]
6pub enum Error {
7    InvalidPath,
8    NotFound(String),
9    Download {
10        url: String,
11        source: reqwest::Error,
12    },
13    InvalidAppImage,
14    InvalidSlug(String),
15    CantUpdatePkg,
16
17    #[from]
18    Io(std::io::Error),
19
20    #[from]
21    Json(serde_json::Error),
22
23    #[from]
24    Http(reqwest::Error),
25
26    #[from]
27    EnvVar(std::env::VarError),
28
29    #[from]
30    IndicatifTemplate(indicatif::style::TemplateError),
31
32    #[from]
33    Octocrab(octocrab::Error),
34
35    #[from]
36    Dialoguer(dialoguer::Error),
37}
38
39impl core::fmt::Display for Error {
40    fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), std::fmt::Error> {
41        match self {
42            Error::Io(e) => write!(fmt, "{e}"),
43            Error::NotFound(name) => write!(fmt, "Application '{name}' not found"),
44            Error::Json(e) => write!(fmt, "JSON error: {e}"),
45            Error::Http(e) => write!(fmt, "HTTP error: {e}"),
46            Error::EnvVar(e) => write!(fmt, "Environment variable error: {e}"),
47            Error::InvalidPath => write!(fmt, "Invalid path provided"),
48            Error::CantUpdatePkg => write!(fmt, "Can't update package"),
49            Error::IndicatifTemplate(e) => write!(fmt, "Progress bar template error: {e}"),
50            Error::Download { url, source } => {
51                if source.is_timeout() {
52                    write!(fmt, "Download timed out from: {url}")
53                } else if source.is_connect() {
54                    write!(fmt, "Failed to connect to: {url}")
55                } else if let Some(status) = source.status() {
56                    match status.as_u16() {
57                        404 => write!(fmt, "AppImage not found at: {url}"),
58                        403 => write!(fmt, "Access denied at: {url}"),
59                        500..=599 => write!(fmt, "Server error when downloading from: {url}"),
60                        _ => write!(fmt, "HTTP {status} error downloading from: {url}"),
61                    }
62                } else {
63                    write!(fmt, "Failed to download from {url}: {source}")
64                }
65            }
66            Error::InvalidAppImage => {
67                write!(fmt, "Invalid AppImage")
68            }
69            Error::InvalidSlug(slug) => write!(fmt, "Invalid repository slug {slug}"),
70            Error::Octocrab(e) => write!(fmt, "Octocrab error: {e}"),
71            Error::Dialoguer(e) => write!(fmt, "Dialoguer error: {e}"),
72        }
73    }
74}
75
76impl std::error::Error for Error {}