cargo_packager/
error.rs

1// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
2// Copyright 2023-2023 CrabNebula Ltd.
3// SPDX-License-Identifier: Apache-2.0
4// SPDX-License-Identifier: MIT
5
6use std::path::PathBuf;
7
8use thiserror::Error;
9
10#[non_exhaustive]
11#[derive(Error, Debug)]
12/// Errors returned by cargo-packager.
13pub enum Error {
14    /// JSON parsing error.
15    #[error(transparent)]
16    Json(#[from] serde_json::Error),
17    /// Target triple architecture error
18    #[error("Unable to determine target-architecture")]
19    Architecture,
20    /// Target triple OS error
21    #[error("Unable to determine target-os")]
22    Os,
23    /// Target triple environment error
24    #[error("Unable to determine target-environment")]
25    Environment,
26    /// I/O errors with path.
27    #[error("I/O Error ({0}): {1}")]
28    IoWithPath(PathBuf, std::io::Error),
29    /// I/O copy file errors.
30    #[error("Failed to copy file from {0} to {1}: {2}")]
31    CopyFile(PathBuf, PathBuf, std::io::Error),
32    /// I/O rename file errors.
33    #[error("Failed to rename file from {0} to {1}: {2}")]
34    RenameFile(PathBuf, PathBuf, std::io::Error),
35    /// I/O symlink file errors.
36    #[error("Failed to symlink file from {0} to {1}: {2}")]
37    Symlink(PathBuf, PathBuf, std::io::Error),
38    /// I/O errors.
39    #[error(transparent)]
40    Io(#[from] std::io::Error),
41    /// Hex de/encoding errors.
42    #[error(transparent)]
43    Hex(#[from] hex::FromHexError),
44    /// Failed to validate downloaded file hash.
45    #[error("Hash mismatch of downloaded file")]
46    HashError,
47    /// Zip error.
48    #[error(transparent)]
49    ZipError(#[from] zip::result::ZipError),
50    /// Zip error.
51    #[error(transparent)]
52    DownloadError(#[from] Box<ureq::Error>),
53    /// Unsupported OS bitness.
54    #[error("Unsupported OS bitness")]
55    UnsupportedBitness,
56    /// Windows SignTool not found.
57    #[error("SignTool not found")]
58    SignToolNotFound,
59    /// Unexpected target triple.
60    #[error("Unexpected target triple: {0}")]
61    UnexpectedTargetTriple(String),
62    /// Unsupported architecture.
63    #[error("Unsupported architecture for \"{0}\" target triple: {0}")]
64    UnsupportedArch(String, String),
65    /// Could not find the main binary in list of provided binaries.
66    #[error("Could not find the main binary in list of provided binaries")]
67    MainBinaryNotFound,
68    /// Semver parsing error
69    #[error(transparent)]
70    Semver(#[from] semver::Error),
71    /// Non-numeric build metadata in app version.
72    #[error("Optional build metadata in app version must be numeric-only {}", .0.clone().unwrap_or_default())]
73    NonNumericBuildMetadata(Option<String>),
74    /// Invalid app version when building [crate::PackageFormat::Wix]
75    #[error("Invalid app version: {0}")]
76    InvalidAppVersion(String),
77    /// Handlebars render error.
78    #[error(transparent)]
79    HandleBarsRenderError(#[from] handlebars::RenderError),
80    /// Handlebars template error.
81    #[error(transparent)]
82    HandleBarsTemplateError(#[from] Box<handlebars::TemplateError>),
83    /// Nsis error
84    #[error("Error running makensis.exe: {0}")]
85    NsisFailed(std::io::Error),
86    /// Nsis error
87    #[error("Error running {0}: {0}")]
88    WixFailed(String, std::io::Error),
89    /// create-dmg script error
90    #[error("Error running create-dmg script: {0}")]
91    CreateDmgFailed(std::io::Error),
92    /// signtool.exe error
93    #[error("Error running signtool.exe: {0}")]
94    SignToolFailed(std::io::Error),
95    /// Custom signing command error
96    #[error("Error running custom signing command: {0}")]
97    CustomSignCommandFailed(std::io::Error),
98    /// bundle_appimage script error
99    #[error("Error running bundle_appimage.sh script: {0}")]
100    AppImageScriptFailed(std::io::Error),
101    /// Failed to get parent directory of a path
102    #[error("Failed to get parent directory of {0}")]
103    ParentDirNotFound(PathBuf),
104    /// A hook, for example `beforePackagaingCommand`, has failed.
105    #[error("{0} `{1}` failed: {2}")]
106    HookCommandFailure(String, String, std::io::Error),
107    /// A hook, for example `beforePackagaingCommand`, has failed with an exit code.
108    #[error("{0} `{1}` failed with exit code {2}")]
109    HookCommandFailureWithExitCode(String, String, i32),
110    /// Regex error.
111    #[cfg(windows)]
112    #[error(transparent)]
113    RegexError(#[from] regex::Error),
114    /// Glob pattern error.
115    #[error(transparent)]
116    GlobPatternError(#[from] glob::PatternError),
117    /// Glob error.
118    #[error(transparent)]
119    Glob(#[from] glob::GlobError),
120    /// Unsupported WiX language
121    #[cfg(windows)]
122    #[error("Wix language {0} not found. It must be one of {1}")]
123    UnsupportedWixLanguage(String, String),
124    /// Image crate errors.
125    #[error(transparent)]
126    ImageError(#[from] image::ImageError),
127    /// walkdir crate errors.
128    #[error(transparent)]
129    WalkDirError(#[from] walkdir::Error),
130    /// Path prefix strip error.
131    #[error(transparent)]
132    StripPrefixError(#[from] std::path::StripPrefixError),
133    /// Relative paths errors
134    #[error(transparent)]
135    RelativeToError(#[from] relative_path::RelativeToError),
136    /// Time error.
137    #[error("`{0}`")]
138    TimeError(#[from] time::error::Error),
139    /// Plist error.
140    #[error(transparent)]
141    Plist(#[from] plist::Error),
142    /// Framework not found.
143    #[error("Framework {0} not found")]
144    FrameworkNotFound(String),
145    /// Invalid framework.
146    #[error("Invalid framework {framework}: {reason}")]
147    InvalidFramework {
148        /// Framework name
149        framework: String,
150        /// Reason why this framework is invalid
151        reason: &'static str,
152    },
153    /// Invalid icons.
154    #[error("Could not find a valid icon")]
155    InvalidIconList,
156    /// Failed to notarize.
157    #[error("Failed to notarize app")]
158    FailedToNotarize,
159    /// Rejected on notarize.
160    #[error("Failed to notarize app: {0}")]
161    NotarizeRejected(String),
162    /// Failed to parse notarytool output.
163    #[error("Failed to parse notarytool output as JSON: `{0}`")]
164    FailedToParseNotarytoolOutput(String),
165    /// Failed to find API key file.
166    #[error("Could not find API key file. Please set the APPLE_API_KEY_PATH environment variables to the path to the {filename} file")]
167    ApiKeyMissing {
168        /// Filename of the API key.
169        filename: String,
170    },
171    /// Missing notarize environment variables.
172    #[error("Could not find APPLE_ID & APPLE_PASSWORD & APPLE_TEAM_ID or APPLE_API_KEY & APPLE_API_ISSUER & APPLE_API_KEY_PATH environment variables found")]
173    MissingNotarizeAuthVars,
174    /// Failed to list keychains
175    #[error("Failed to list keychains: {0}")]
176    FailedToListKeyChain(std::io::Error),
177    /// Failed to decode certficate as base64
178    #[error("Failed to decode certficate as base64: {0}")]
179    FailedToDecodeCert(std::io::Error),
180    /// Failed to create keychain.
181    #[error("Failed to create keychain: {0}")]
182    FailedToCreateKeyChain(std::io::Error),
183    /// Failed to create keychain.
184    #[error("Failed to unlock keychain: {0}")]
185    FailedToUnlockKeyChain(std::io::Error),
186    /// Failed to import certificate.
187    #[error("Failed to import certificate: {0}")]
188    FailedToImportCert(std::io::Error),
189    /// Failed to set keychain settings.
190    #[error("Failed to set keychain settings: {0}")]
191    FailedToSetKeychainSettings(std::io::Error),
192    /// Failed to set key partition list.
193    #[error("Failed to set key partition list: {0}")]
194    FailedToSetKeyPartitionList(std::io::Error),
195    /// Failed to run codesign utility.
196    #[error("Failed to run codesign utility: {0}")]
197    FailedToRunCodesign(std::io::Error),
198    /// Failed to run ditto utility.
199    #[error("Failed to run ditto utility: {0}")]
200    FailedToRunDitto(std::io::Error),
201    /// Failed to run xcrun utility.
202    #[error("Failed to run xcrun utility: {0}")]
203    FailedToRunXcrun(std::io::Error),
204    /// Path already exists.
205    #[error("{0} already exists")]
206    AlreadyExists(PathBuf),
207    /// Path does not exist.
208    #[error("{0} does not exist")]
209    DoesNotExist(PathBuf),
210    /// Path is not a directory.
211    #[error("{0} is not a directory")]
212    IsNotDirectory(PathBuf),
213    /// Could not find a square icon to use as AppImage icon
214    #[error("Could not find a square icon to use as AppImage icon")]
215    AppImageSquareIcon,
216    /// Base64 decoding error.
217    #[error(transparent)]
218    Base64DecodeError(#[from] base64::DecodeError),
219    /// Utf8 parsing error.
220    #[error(transparent)]
221    Utf8Error(#[from] std::str::Utf8Error),
222    /// minisign errors.
223    #[error(transparent)]
224    Minisign(#[from] minisign::PError),
225    /// System time errors.
226    #[error(transparent)]
227    SystemTimeError(#[from] std::time::SystemTimeError),
228    /// Signing keys generation error.
229    #[error("aborted key generation, {0} already exists and force overrwite wasnot desired.")]
230    SigningKeyExists(PathBuf),
231    /// Failed to extract external binary filename
232    #[error("Failed to extract filename from {0}")]
233    FailedToExtractFilename(PathBuf),
234    /// Failed to remove extended attributes from app bundle
235    #[error("Failed to remove extended attributes from app bundle: {0}")]
236    #[cfg(target_os = "macos")]
237    FailedToRemoveExtendedAttributes(std::io::Error),
238    /// Could not find the embedded.provisionprofile file.
239    #[error("Embedded provision profile file {0} not found")]
240    EmbeddedProvisionprofileFileNotFound(PathBuf),
241    /// Could not copy the embedded.provisionprofile file to the Contents directory.
242    #[error("Could not copy embedded provision profile file {0}: {1}")]
243    FailedToCopyEmbeddedProvisionprofile(PathBuf, std::io::Error),
244    /// Failed to open Windows registry.
245    #[error("failed to open registry {0}")]
246    OpenRegistry(String),
247    /// Failed to get registry value.
248    #[error("failed to get {0} value on registry")]
249    GetRegistryValue(String),
250    /// Failed to enumerate registry keys.
251    #[error("failed to enumerate registry keys")]
252    FailedToEnumerateRegKeys,
253}
254
255/// Convenient type alias of Result type for cargo-packager.
256pub type Result<T> = std::result::Result<T, Error>;