Skip to content

Commit b06b3bd

Browse files
refactor(cli): improve errors (#14126)
* refactor(cli): improve errors * update change files * license * add errorext with fs_context helper * update linux * lint * fmt * windows * revert bundler breaking change * fix ios mod * ref * reduce amount of enum variants * fix macos build * Fix windows build * Clippy * capitalize cargo [skip ci] --------- Co-authored-by: Tony <[email protected]>
1 parent eb60b99 commit b06b3bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1952
-996
lines changed

.changes/improve-errors.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@tauri-apps/cli": minor:enhance
3+
"tauri-cli": minor:enhance
4+
"tauri-bundler": minor:enhance
5+
---
6+
7+
Improve error messages with more context.
8+

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bench/src/run_benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn main() -> Result<()> {
375375
if let Some(filename) = bench_file.to_str() {
376376
utils::write_json(filename, &serde_json::to_value(&new_data)?)
377377
.context("failed to write benchmark results to file")?;
378-
println!("Results written to: {}", filename);
378+
println!("Results written to: {filename}");
379379
} else {
380380
eprintln!("Cannot write bench.json, path contains invalid UTF-8");
381381
}

crates/tauri-bundler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ tauri-utils = { version = "2.7.0", path = "../tauri-utils", features = [
2020
] }
2121
image = "0.25"
2222
flate2 = "1"
23-
anyhow = "1"
2423
thiserror = "2"
24+
anyhow = "1"
2525
serde_json = "1"
2626
serde = { version = "1", features = ["derive"] }
2727
strsim = "0.11"

crates/tauri-bundler/src/bundle.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ pub use self::{
4949
Settings, SettingsBuilder, Size, UpdaterSettings,
5050
},
5151
};
52-
#[cfg(target_os = "macos")]
53-
use anyhow::Context;
5452
pub use settings::{NsisSettings, WindowsSettings, WixLanguage, WixLanguageConfig, WixSettings};
5553

5654
use std::{
@@ -223,24 +221,24 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<Bundle>> {
223221
.map(|b| b.bundle_paths)
224222
{
225223
for app_bundle_path in &app_bundle_paths {
224+
use crate::error::ErrorExt;
225+
226226
log::info!(action = "Cleaning"; "{}", app_bundle_path.display());
227227
match app_bundle_path.is_dir() {
228228
true => std::fs::remove_dir_all(app_bundle_path),
229229
false => std::fs::remove_file(app_bundle_path),
230230
}
231-
.with_context(|| {
232-
format!(
233-
"Failed to clean the app bundle at {}",
234-
app_bundle_path.display()
235-
)
236-
})?
231+
.fs_context(
232+
"failed to clean the app bundle",
233+
app_bundle_path.to_path_buf(),
234+
)?;
237235
}
238236
}
239237
}
240238
}
241239

242240
if bundles.is_empty() {
243-
return Err(anyhow::anyhow!("No bundles were built").into());
241+
return Ok(bundles);
244242
}
245243

246244
let bundles_wo_updater = bundles

crates/tauri-bundler/src/bundle/linux/appimage.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use super::debian;
77
use crate::{
88
bundle::settings::Arch,
9+
error::{Context, ErrorExt},
910
utils::{fs_utils, http_utils::download, CommandExt},
1011
Settings,
1112
};
12-
use anyhow::Context;
1313
use std::{
1414
fs,
1515
path::{Path, PathBuf},
@@ -124,13 +124,13 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
124124
// xdg-open will be handled by the `files` config instead
125125
if settings.deep_link_protocols().is_some() && !app_dir_usr_bin.join("xdg-open").exists() {
126126
fs::copy("/usr/bin/xdg-mime", app_dir_usr_bin.join("xdg-mime"))
127-
.context("xdg-mime binary not found")?;
127+
.fs_context("xdg-mime binary not found", "/usr/bin/xdg-mime".to_string())?;
128128
}
129129

130130
// we also check if the user may have provided their own copy already
131131
if settings.appimage().bundle_xdg_open && !app_dir_usr_bin.join("xdg-open").exists() {
132132
fs::copy("/usr/bin/xdg-open", app_dir_usr_bin.join("xdg-open"))
133-
.context("xdg-open binary not found")?;
133+
.fs_context("xdg-open binary not found", "/usr/bin/xdg-open".to_string())?;
134134
}
135135

136136
let search_dirs = [

crates/tauri-bundler/src/bundle/linux/debian.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@
2424
// generate postinst or prerm files.
2525

2626
use super::freedesktop;
27-
use crate::{bundle::settings::Arch, utils::fs_utils, Settings};
28-
use anyhow::Context;
27+
use crate::{
28+
bundle::settings::Arch,
29+
error::{Context, ErrorExt},
30+
utils::fs_utils,
31+
Settings,
32+
};
2933
use flate2::{write::GzEncoder, Compression};
3034
use tar::HeaderMode;
3135
use walkdir::WalkDir;
@@ -64,30 +68,32 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
6468
let base_dir = settings.project_out_directory().join("bundle/deb");
6569
let package_dir = base_dir.join(&package_base_name);
6670
if package_dir.exists() {
67-
fs::remove_dir_all(&package_dir)
68-
.with_context(|| format!("Failed to remove old {package_base_name}"))?;
71+
fs::remove_dir_all(&package_dir).fs_context(
72+
"Failed to Remove old package directory",
73+
package_dir.clone(),
74+
)?;
6975
}
7076
let package_path = base_dir.join(&package_name);
7177

7278
log::info!(action = "Bundling"; "{} ({})", package_name, package_path.display());
7379

74-
let (data_dir, _) = generate_data(settings, &package_dir)
75-
.with_context(|| "Failed to build data folders and files")?;
80+
let (data_dir, _) =
81+
generate_data(settings, &package_dir).context("Failed to build data folders and files")?;
7682
fs_utils::copy_custom_files(&settings.deb().files, &data_dir)
77-
.with_context(|| "Failed to copy custom files")?;
83+
.context("Failed to copy custom files")?;
7884

7985
// Generate control files.
8086
let control_dir = package_dir.join("control");
8187
generate_control_file(settings, arch, &control_dir, &data_dir)
82-
.with_context(|| "Failed to create control file")?;
83-
generate_scripts(settings, &control_dir).with_context(|| "Failed to create control scripts")?;
84-
generate_md5sums(&control_dir, &data_dir).with_context(|| "Failed to create md5sums file")?;
88+
.context("Failed to create control file")?;
89+
generate_scripts(settings, &control_dir).context("Failed to create control scripts")?;
90+
generate_md5sums(&control_dir, &data_dir).context("Failed to create md5sums file")?;
8591

8692
// Generate `debian-binary` file; see
8793
// http://www.tldp.org/HOWTO/Debian-Binary-Package-Building-HOWTO/x60.html#AEN66
8894
let debian_binary_path = package_dir.join("debian-binary");
8995
create_file_with_data(&debian_binary_path, "2.0\n")
90-
.with_context(|| "Failed to create debian-binary file")?;
96+
.context("Failed to create debian-binary file")?;
9197

9298
// Apply tar/gzip/ar to create the final package file.
9399
let control_tar_gz_path =

crates/tauri-bundler/src/bundle/linux/freedesktop/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ use std::fs::{read_to_string, File};
2121
use std::io::BufReader;
2222
use std::path::{Path, PathBuf};
2323

24-
use anyhow::Context;
2524
use handlebars::Handlebars;
2625
use image::{self, codecs::png::PngDecoder, ImageDecoder};
2726
use serde::Serialize;
2827

2928
use crate::{
29+
error::Context,
3030
utils::{self, fs_utils},
3131
Settings,
3232
};
@@ -114,11 +114,13 @@ pub fn generate_desktop_file(
114114
if let Some(template) = custom_template_path {
115115
handlebars
116116
.register_template_string("main.desktop", read_to_string(template)?)
117-
.with_context(|| "Failed to setup custom handlebar template")?;
117+
.map_err(Into::into)
118+
.context("Failed to setup custom handlebar template")?;
118119
} else {
119120
handlebars
120121
.register_template_string("main.desktop", include_str!("./main.desktop"))
121-
.with_context(|| "Failed to setup default handlebar template")?;
122+
.map_err(Into::into)
123+
.context("Failed to setup default handlebar template")?;
122124
}
123125

124126
#[derive(Serialize)]

crates/tauri-bundler/src/bundle/linux/rpm.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
// SPDX-License-Identifier: Apache-2.0
44
// SPDX-License-Identifier: MIT
55

6-
use crate::{bundle::settings::Arch, Settings};
6+
use crate::{bundle::settings::Arch, error::ErrorExt, Settings};
77

8-
use anyhow::Context;
98
use rpm::{self, signature::pgp, Dependency, FileMode, FileOptions};
109
use std::{
1110
env,
@@ -48,10 +47,13 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
4847
let base_dir = settings.project_out_directory().join("bundle/rpm");
4948
let package_dir = base_dir.join(&package_base_name);
5049
if package_dir.exists() {
51-
fs::remove_dir_all(&package_dir)
52-
.with_context(|| format!("Failed to remove old {package_base_name}"))?;
50+
fs::remove_dir_all(&package_dir).fs_context(
51+
"Failed to remove old package directory",
52+
package_dir.clone(),
53+
)?;
5354
}
54-
fs::create_dir_all(&package_dir)?;
55+
fs::create_dir_all(&package_dir)
56+
.fs_context("Failed to create package directory", package_dir.clone())?;
5557
let package_path = base_dir.join(&package_name);
5658

5759
log::info!(action = "Bundling"; "{} ({})", package_name, package_path.display());

crates/tauri-bundler/src/bundle/macos/app.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424

2525
use super::{
2626
icon::create_icns_file,
27-
sign::{notarize, notarize_auth, notarize_without_stapling, sign, NotarizeAuthError, SignTarget},
27+
sign::{notarize, notarize_auth, notarize_without_stapling, sign, SignTarget},
2828
};
2929
use crate::{
30+
error::{Context, ErrorExt, NotarizeAuthError},
3031
utils::{fs_utils, CommandExt},
3132
Error::GenericError,
3233
Settings,
3334
};
3435

35-
use anyhow::Context;
36-
3736
use std::{
3837
ffi::OsStr,
3938
fs,
@@ -65,12 +64,16 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
6564
log::info!(action = "Bundling"; "{} ({})", app_product_name, app_bundle_path.display());
6665

6766
if app_bundle_path.exists() {
68-
fs::remove_dir_all(&app_bundle_path)
69-
.with_context(|| format!("Failed to remove old {app_product_name}"))?;
67+
fs::remove_dir_all(&app_bundle_path).fs_context(
68+
"failed to remove old app bundle",
69+
app_bundle_path.to_path_buf(),
70+
)?;
7071
}
7172
let bundle_directory = app_bundle_path.join("Contents");
72-
fs::create_dir_all(&bundle_directory)
73-
.with_context(|| format!("Failed to create bundle directory at {bundle_directory:?}"))?;
73+
fs::create_dir_all(&bundle_directory).fs_context(
74+
"failed to create bundle directory",
75+
bundle_directory.to_path_buf(),
76+
)?;
7477

7578
let resources_dir = bundle_directory.join("Resources");
7679
let bin_dir = bundle_directory.join("MacOS");
@@ -134,7 +137,7 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
134137
}
135138
Err(e) => {
136139
if matches!(e, NotarizeAuthError::MissingTeamId) {
137-
return Err(anyhow::anyhow!("{e}").into());
140+
return Err(e.into());
138141
} else {
139142
log::warn!("skipping app notarization, {}", e.to_string());
140143
}
@@ -401,8 +404,10 @@ fn copy_frameworks_to_bundle(
401404
return Ok(paths);
402405
}
403406
let dest_dir = bundle_directory.join("Frameworks");
404-
fs::create_dir_all(bundle_directory)
405-
.with_context(|| format!("Failed to create Frameworks directory at {dest_dir:?}"))?;
407+
fs::create_dir_all(&dest_dir).fs_context(
408+
"failed to create Frameworks directory",
409+
dest_dir.to_path_buf(),
410+
)?;
406411
for framework in frameworks.iter() {
407412
if framework.ends_with(".framework") {
408413
let src_path = PathBuf::from(framework);

0 commit comments

Comments
 (0)