tauri_bundler/
error.rs

1// Copyright 2016-2019 Cargo-Bundle developers <https://github.com/burtonageo/cargo-bundle>
2// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
3// SPDX-License-Identifier: Apache-2.0
4// SPDX-License-Identifier: MIT
5
6use std::{
7  fmt::Display,
8  io, num,
9  path::{self, PathBuf},
10};
11use thiserror::Error as DeriveError;
12
13/// Errors returned by the bundler.
14#[derive(Debug, DeriveError)]
15#[non_exhaustive]
16pub enum Error {
17  /// Error with context. Created by the [`Context`] trait.
18  #[error("{0}: {1}")]
19  Context(String, Box<Self>),
20  /// File system error.
21  #[error("{context} {path}: {error}")]
22  Fs {
23    /// Context of the error.
24    context: &'static str,
25    /// Path that was accessed.
26    path: PathBuf,
27    /// Error that occurred.
28    error: io::Error,
29  },
30  /// Child process error.
31  #[error("failed to run command {command}: {error}")]
32  CommandFailed {
33    /// Command that failed.
34    command: String,
35    /// Error that occurred.
36    error: io::Error,
37  },
38  /// Error running tauri_utils API.
39  #[error("{0}")]
40  Resource(#[from] tauri_utils::Error),
41  /// Bundler error.
42  ///
43  /// This variant is no longer used as this crate no longer uses anyhow.
44  // TODO(v3): remove this variant
45  #[error("{0:#}")]
46  BundlerError(#[from] anyhow::Error),
47  /// I/O error.
48  #[error("`{0}`")]
49  IoError(#[from] io::Error),
50  /// Image error.
51  #[error("`{0}`")]
52  ImageError(#[from] image::ImageError),
53  /// Error walking directory.
54  #[error("`{0}`")]
55  WalkdirError(#[from] walkdir::Error),
56  /// Strip prefix error.
57  #[error("`{0}`")]
58  StripError(#[from] path::StripPrefixError),
59  /// Number parse error.
60  #[error("`{0}`")]
61  ConvertError(#[from] num::TryFromIntError),
62  /// Zip error.
63  #[error("`{0}`")]
64  ZipError(#[from] zip::result::ZipError),
65  /// Hex error.
66  #[error("`{0}`")]
67  HexError(#[from] hex::FromHexError),
68  /// Handlebars template error.
69  #[error("`{0}`")]
70  HandleBarsError(#[from] handlebars::RenderError),
71  /// JSON error.
72  #[error("`{0}`")]
73  JsonError(#[from] serde_json::error::Error),
74  /// Regex error.
75  #[cfg(any(target_os = "macos", windows))]
76  #[error("`{0}`")]
77  RegexError(#[from] regex::Error),
78  /// Failed to perform HTTP request.
79  #[error("`{0}`")]
80  HttpError(#[from] Box<ureq::Error>),
81  /// Invalid glob pattern.
82  #[cfg(windows)]
83  #[error("{0}")]
84  GlobPattern(#[from] glob::PatternError),
85  /// Failed to use glob pattern.
86  #[cfg(windows)]
87  #[error("`{0}`")]
88  Glob(#[from] glob::GlobError),
89  /// Failed to parse the URL
90  #[error("`{0}`")]
91  UrlParse(#[from] url::ParseError),
92  /// Failed to validate downloaded file hash.
93  #[error("hash mismatch of downloaded file")]
94  HashError,
95  /// Failed to parse binary
96  #[error("Binary parse error: `{0}`")]
97  BinaryParseError(#[from] goblin::error::Error),
98  /// Package type is not supported by target platform
99  #[error("Wrong package type {0} for platform {1}")]
100  InvalidPackageType(String, String),
101  /// Bundle type symbol missing in binary
102  #[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  /// Failed to write binary file changed
112  #[error("Failed to write binary file changes: `{0}`")]
113  BinaryWriteError(String),
114  /// Invalid offset while patching binary file
115  #[error("Invalid offset while patching binary file")]
116  BinaryOffsetOutOfRange,
117  /// Unsupported architecture.
118  #[error("Architecture Error: `{0}`")]
119  ArchError(String),
120  /// Couldn't find icons.
121  #[error("Could not find Icon paths.  Please make sure they exist in the tauri config JSON file")]
122  IconPathError,
123  /// Couldn't find background file.
124  #[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 on path util operation.
127  #[error("Path Error:`{0}`")]
128  PathUtilError(String),
129  /// Error on shell script.
130  #[error("Shell Scripting Error:`{0}`")]
131  ShellScriptError(String),
132  /// Generic error.
133  #[error("`{0}`")]
134  GenericError(String),
135  /// No bundled project found for the updater.
136  #[error("Unable to find a bundled project for the updater")]
137  UnableToFindProject,
138  /// String is not UTF-8.
139  #[error("string is not UTF-8")]
140  Utf8(#[from] std::str::Utf8Error),
141  /// Windows SignTool not found.
142  #[error("SignTool not found")]
143  SignToolNotFound,
144  /// Failed to open Windows registry.
145  #[error("failed to open registry {0}")]
146  OpenRegistry(String),
147  /// Failed to get registry value.
148  #[error("failed to get {0} value on registry")]
149  GetRegistryValue(String),
150  /// Failed to enumerate registry keys.
151  #[error("failed to enumerate registry keys")]
152  FailedToEnumerateRegKeys,
153  /// Unsupported OS bitness.
154  #[error("unsupported OS bitness")]
155  UnsupportedBitness,
156  /// Failed to sign application.
157  #[error("failed to sign app: {0}")]
158  Sign(String),
159  /// time error.
160  #[cfg(target_os = "macos")]
161  #[error("`{0}`")]
162  TimeError(#[from] time::error::Error),
163  /// Plist error.
164  #[cfg(target_os = "macos")]
165  #[error(transparent)]
166  Plist(#[from] plist::Error),
167  /// Rpm error.
168  #[cfg(target_os = "linux")]
169  #[error("{0}")]
170  RpmError(#[from] rpm::Error),
171  /// Failed to notarize application.
172  #[cfg(target_os = "macos")]
173  #[error("failed to notarize app: {0}")]
174  AppleNotarization(#[from] NotarizeAuthError),
175  /// Failed to codesign application.
176  #[cfg(target_os = "macos")]
177  #[error("failed codesign application: {0}")]
178  AppleCodesign(#[from] Box<tauri_macos_sign::Error>),
179  /// Handlebars template error.
180  #[error(transparent)]
181  Template(#[from] handlebars::TemplateError),
182  /// Semver error.
183  #[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
201/// Convenient type alias of Result type.
202pub type Result<T> = std::result::Result<T, Error>;
203
204pub trait Context<T> {
205  // Required methods
206  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;