Skip to main content

hypersig_desktop/
error.rs

1//! What can stop the application from starting or running.
2
3use std::error::Error as StdError;
4use std::fmt;
5use std::io;
6
7/// Why an operation of this crate failed.
8///
9/// [`App::run`](crate::App::run), [`App::data_dir`](crate::App::data_dir), and
10/// the methods of [`Os`](crate::Os), [`Windows`](crate::Windows),
11/// [`Window`](crate::Window), [`Clipboard`](crate::Clipboard), and the
12/// updater return it.
13pub struct Error {
14    kind: ErrorKind,
15    source: Option<Box<dyn StdError + Send + Sync>>,
16}
17
18/// The kind of an [`Error`].
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20#[non_exhaustive]
21pub enum ErrorKind {
22    /// The environment does not say where the application's directory is.
23    NoDataDir,
24    /// Another copy of the application runs and did not take this start's command line.
25    AlreadyRunning,
26    /// The future given to [`App::run`](crate::App::run) returned an error.
27    Setup,
28    /// The window or its web view could not be made or could not load.
29    Window,
30    /// A file, the port, or the server failed.
31    Io,
32    /// The application is closing, so it opens and changes no window.
33    Closed,
34    /// A check of a smoke run failed.
35    Smoke,
36    /// The system clipboard could not be read or written.
37    Clipboard,
38    /// The system did not show a notification.
39    Notification,
40    /// The entry that starts the application at login could not be read
41    /// or written.
42    Autostart,
43    /// The tray icon could not be made, or its PNG could not be read.
44    Tray,
45    /// The system did not register a global shortcut, as when another
46    /// application holds it.
47    Shortcut,
48    /// [`Os::open_external`](crate::Os::open_external) got a URL without a scheme.
49    ///
50    /// The system would open such a URL as a file.
51    NotExternal,
52    /// Finding, downloading, verifying, or installing a new version failed.
53    Update,
54    /// [`App::run`](crate::App::run) was called inside a Tokio runtime.
55    ///
56    /// `run` starts a runtime of its own, so `main` is not `#[tokio::main]`.
57    InRuntime,
58}
59
60impl Error {
61    /// What went wrong.
62    #[must_use]
63    pub fn kind(&self) -> ErrorKind {
64        self.kind
65    }
66
67    pub(crate) fn no_data_dir() -> Self {
68        Self {
69            kind: ErrorKind::NoDataDir,
70            source: None,
71        }
72    }
73
74    pub(crate) fn already_running() -> Self {
75        Self {
76            kind: ErrorKind::AlreadyRunning,
77            source: None,
78        }
79    }
80
81    pub(crate) fn in_runtime() -> Self {
82        Self {
83            kind: ErrorKind::InRuntime,
84            source: None,
85        }
86    }
87
88    pub(crate) fn closed() -> Self {
89        Self {
90            kind: ErrorKind::Closed,
91            source: None,
92        }
93    }
94
95    pub(crate) fn smoke(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
96        Self {
97            kind: ErrorKind::Smoke,
98            source: Some(why.into()),
99        }
100    }
101
102    pub(crate) fn clipboard(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
103        Self {
104            kind: ErrorKind::Clipboard,
105            source: Some(why.into()),
106        }
107    }
108
109    pub(crate) fn shortcut(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
110        Self {
111            kind: ErrorKind::Shortcut,
112            source: Some(why.into()),
113        }
114    }
115
116    pub(crate) fn autostart(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
117        Self {
118            kind: ErrorKind::Autostart,
119            source: Some(why.into()),
120        }
121    }
122
123    pub(crate) fn tray(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
124        Self {
125            kind: ErrorKind::Tray,
126            source: Some(why.into()),
127        }
128    }
129
130    pub(crate) fn notification(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
131        Self {
132            kind: ErrorKind::Notification,
133            source: Some(why.into()),
134        }
135    }
136
137    #[cfg(feature = "updater")]
138    pub(crate) fn update(why: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
139        Self {
140            kind: ErrorKind::Update,
141            source: Some(why.into()),
142        }
143    }
144
145    pub(crate) fn not_external() -> Self {
146        Self {
147            kind: ErrorKind::NotExternal,
148            source: None,
149        }
150    }
151
152    pub(crate) fn setup(source: Box<dyn StdError + Send + Sync>) -> Self {
153        Self {
154            kind: ErrorKind::Setup,
155            source: Some(source),
156        }
157    }
158
159    pub(crate) fn window(source: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
160        Self {
161            kind: ErrorKind::Window,
162            source: Some(source.into()),
163        }
164    }
165}
166
167impl From<io::Error> for Error {
168    fn from(source: io::Error) -> Self {
169        Self {
170            kind: ErrorKind::Io,
171            source: Some(source.into()),
172        }
173    }
174}
175
176impl fmt::Display for Error {
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        let what = match self.kind {
179            ErrorKind::NoDataDir => {
180                "no directory for the application's files: HOME, LOCALAPPDATA, or XDG_DATA_HOME is unset or not an absolute path"
181            }
182            ErrorKind::AlreadyRunning => "another copy of this application is running",
183            ErrorKind::Setup => "the application's setup failed",
184            ErrorKind::Window => "the window could not be shown",
185            ErrorKind::Io => "the application's files or server failed",
186            ErrorKind::Closed => "the application is closing",
187            ErrorKind::InRuntime => {
188                "`App::run` was called inside a Tokio runtime; call it from a `main` without `#[tokio::main]`"
189            }
190            ErrorKind::Smoke => "the smoke run failed",
191            ErrorKind::Clipboard => "the clipboard could not be used",
192            ErrorKind::Notification => "the notification could not be shown",
193            ErrorKind::Tray => "the tray icon could not be shown",
194            ErrorKind::Autostart => "the start at login could not be read or changed",
195            ErrorKind::Shortcut => "the global shortcut could not be registered",
196            ErrorKind::Update => "the new version could not be found or installed",
197            ErrorKind::NotExternal => {
198                "the URL names no scheme, so it is not opened in another program"
199            }
200        };
201        match &self.source {
202            Some(source) => write!(f, "{what}: {source}"),
203            None => f.write_str(what),
204        }
205    }
206}
207
208/// The message, as [`Display`](fmt::Display) writes it, because `main`
209/// prints the `Debug` form of the error it returns.
210impl fmt::Debug for Error {
211    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212        fmt::Display::fmt(self, f)
213    }
214}
215
216impl StdError for Error {
217    fn source(&self) -> Option<&(dyn StdError + 'static)> {
218        Some(self.source.as_deref()?)
219    }
220}