1use std::{
7 fmt::Display,
8 io, num,
9 path::{self, PathBuf},
10};
11use thiserror::Error as DeriveError;
12
13#[derive(Debug, DeriveError)]
15#[non_exhaustive]
16pub enum Error {
17 #[error("{0}: {1}")]
19 Context(String, Box<Self>),
20 #[error("{context} {path}: {error}")]
22 Fs {
23 context: &'static str,
25 path: PathBuf,
27 error: io::Error,
29 },
30 #[error("failed to run command {command}: {error}")]
32 CommandFailed {
33 command: String,
35 error: io::Error,
37 },
38 #[error("{0}")]
40 Resource(#[from] tauri_utils::Error),
41 #[error("{0:#}")]
46 BundlerError(#[from] anyhow::Error),
47 #[error("`{0}`")]
49 IoError(#[from] io::Error),
50 #[error("`{0}`")]
52 ImageError(#[from] image::ImageError),
53 #[error("`{0}`")]
55 WalkdirError(#[from] walkdir::Error),
56 #[error("`{0}`")]
58 StripError(#[from] path::StripPrefixError),
59 #[error("`{0}`")]
61 ConvertError(#[from] num::TryFromIntError),
62 #[error("`{0}`")]
64 ZipError(#[from] zip::result::ZipError),
65 #[error("`{0}`")]
67 HexError(#[from] hex::FromHexError),
68 #[error("`{0}`")]
70 HandleBarsError(#[from] handlebars::RenderError),
71 #[error("`{0}`")]
73 JsonError(#[from] serde_json::error::Error),
74 #[cfg(any(target_os = "macos", windows))]
76 #[error("`{0}`")]
77 RegexError(#[from] regex::Error),
78 #[error("`{0}`")]
80 HttpError(#[from] Box<ureq::Error>),
81 #[cfg(windows)]
83 #[error("{0}")]
84 GlobPattern(#[from] glob::PatternError),
85 #[cfg(windows)]
87 #[error("`{0}`")]
88 Glob(#[from] glob::GlobError),
89 #[error("`{0}`")]
91 UrlParse(#[from] url::ParseError),
92 #[error("hash mismatch of downloaded file")]
94 HashError,
95 #[error("Binary parse error: `{0}`")]
97 BinaryParseError(#[from] goblin::error::Error),
98 #[error("Wrong package type {0} for platform {1}")]
100 InvalidPackageType(String, String),
101 #[cfg_attr(
103 target_os = "linux",
104 error("__TAURI_BUNDLE_TYPE variable not found in binary. Make sure tauri crate and tauri-cli are up to date and that symbol stripping is disabled (https://doc.rust-lang.org/cargo/reference/profiles.html#strip)")
105 )]
106 #[cfg_attr(
107 not(target_os = "linux"),
108 error("__TAURI_BUNDLE_TYPE variable not found in binary. Make sure tauri crate and tauri-cli are up to date")
109 )]
110 MissingBundleTypeVar,
111 #[error("Failed to write binary file changes: `{0}`")]
113 BinaryWriteError(String),
114 #[error("Invalid offset while patching binary file")]
116 BinaryOffsetOutOfRange,
117 #[error("Architecture Error: `{0}`")]
119 ArchError(String),
120 #[error("Could not find Icon paths. Please make sure they exist in the tauri config JSON file")]
122 IconPathError,
123 #[error("Could not find background file. Make sure it exists in the tauri config JSON file and extension is png/jpg/gif")]
125 BackgroundPathError,
126 #[error("Path Error:`{0}`")]
128 PathUtilError(String),
129 #[error("Shell Scripting Error:`{0}`")]
131 ShellScriptError(String),
132 #[error("`{0}`")]
134 GenericError(String),
135 #[error("Unable to find a bundled project for the updater")]
137 UnableToFindProject,
138 #[error("string is not UTF-8")]
140 Utf8(#[from] std::str::Utf8Error),
141 #[error("SignTool not found")]
143 SignToolNotFound,
144 #[error("failed to open registry {0}")]
146 OpenRegistry(String),
147 #[error("failed to get {0} value on registry")]
149 GetRegistryValue(String),
150 #[error("failed to enumerate registry keys")]
152 FailedToEnumerateRegKeys,
153 #[error("unsupported OS bitness")]
155 UnsupportedBitness,
156 #[error("failed to sign app: {0}")]
158 Sign(String),
159 #[cfg(target_os = "macos")]
161 #[error("`{0}`")]
162 TimeError(#[from] time::error::Error),
163 #[cfg(target_os = "macos")]
165 #[error(transparent)]
166 Plist(#[from] plist::Error),
167 #[cfg(target_os = "linux")]
169 #[error("{0}")]
170 RpmError(#[from] rpm::Error),
171 #[cfg(target_os = "macos")]
173 #[error("failed to notarize app: {0}")]
174 AppleNotarization(#[from] NotarizeAuthError),
175 #[cfg(target_os = "macos")]
177 #[error("failed codesign application: {0}")]
178 AppleCodesign(#[from] Box<tauri_macos_sign::Error>),
179 #[error(transparent)]
181 Template(#[from] handlebars::TemplateError),
182 #[error("`{0}`")]
184 SemverError(#[from] semver::Error),
185}
186
187#[cfg(target_os = "macos")]
188#[allow(clippy::enum_variant_names)]
189#[derive(Debug, thiserror::Error)]
190pub enum NotarizeAuthError {
191 #[error(
192 "The team ID is now required for notarization with app-specific password as authentication. Please set the `APPLE_TEAM_ID` environment variable. You can find the team ID in https://developer.apple.com/account#MembershipDetailsCard."
193 )]
194 MissingTeamId,
195 #[error("could not find API key file. Please set the APPLE_API_KEY_PATH environment variables to the path to the {file_name} file")]
196 MissingApiKey { file_name: String },
197 #[error("no APPLE_ID & APPLE_PASSWORD & APPLE_TEAM_ID or APPLE_API_KEY & APPLE_API_ISSUER & APPLE_API_KEY_PATH environment variables found")]
198 MissingCredentials,
199}
200
201pub type Result<T> = std::result::Result<T, Error>;
203
204pub trait Context<T> {
205 fn context<C>(self, context: C) -> Result<T>
207 where
208 C: Display + Send + Sync + 'static;
209 fn with_context<C, F>(self, f: F) -> Result<T>
210 where
211 C: Display + Send + Sync + 'static,
212 F: FnOnce() -> C;
213}
214
215impl<T> Context<T> for Result<T> {
216 fn context<C>(self, context: C) -> Result<T>
217 where
218 C: Display + Send + Sync + 'static,
219 {
220 self.map_err(|e| Error::Context(context.to_string(), Box::new(e)))
221 }
222
223 fn with_context<C, F>(self, f: F) -> Result<T>
224 where
225 C: Display + Send + Sync + 'static,
226 F: FnOnce() -> C,
227 {
228 self.map_err(|e| Error::Context(f().to_string(), Box::new(e)))
229 }
230}
231
232impl<T> Context<T> for Option<T> {
233 fn context<C>(self, context: C) -> Result<T>
234 where
235 C: Display + Send + Sync + 'static,
236 {
237 self.ok_or_else(|| Error::GenericError(context.to_string()))
238 }
239
240 fn with_context<C, F>(self, f: F) -> Result<T>
241 where
242 C: Display + Send + Sync + 'static,
243 F: FnOnce() -> C,
244 {
245 self.ok_or_else(|| Error::GenericError(f().to_string()))
246 }
247}
248
249pub trait ErrorExt<T> {
250 fn fs_context(self, context: &'static str, path: impl Into<PathBuf>) -> Result<T>;
251}
252
253impl<T> ErrorExt<T> for std::result::Result<T, std::io::Error> {
254 fn fs_context(self, context: &'static str, path: impl Into<PathBuf>) -> Result<T> {
255 self.map_err(|error| Error::Fs {
256 context,
257 path: path.into(),
258 error,
259 })
260 }
261}
262
263#[allow(unused)]
264macro_rules! bail {
265 ($msg:literal $(,)?) => {
266 return Err(crate::Error::GenericError($msg.into()))
267 };
268 ($err:expr $(,)?) => {
269 return Err(crate::Error::GenericError($err))
270 };
271 ($fmt:expr, $($arg:tt)*) => {
272 return Err(crate::Error::GenericError(format!($fmt, $($arg)*)))
273 };
274}
275
276#[allow(unused)]
277pub(crate) use bail;