Skip to content

Commit 5cc4b11

Browse files
authored
feat(cli.rs): add context to errors (#1674)
1 parent 9fadbf3 commit 5cc4b11

8 files changed

Lines changed: 69 additions & 26 deletions

File tree

.changes/cli-error-logging.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Improve error logging.

tooling/cli.rs/src/build.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use anyhow::Context;
56
use tauri_bundler::bundle::{bundle_project, PackageType, SettingsBuilder};
67

78
use crate::helpers::{
@@ -67,7 +68,7 @@ impl Build {
6768
let config = get_config(self.config.as_deref())?;
6869

6970
let tauri_path = tauri_dir();
70-
set_current_dir(&tauri_path)?;
71+
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
7172

7273
rewrite_manifest(config.clone())?;
7374

@@ -83,14 +84,16 @@ impl Build {
8384
.arg("/C")
8485
.arg(before_build)
8586
.current_dir(app_dir()),
86-
)?;
87+
)
88+
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?;
8789
#[cfg(not(target_os = "windows"))]
8890
execute_with_output(
8991
&mut Command::new("sh")
9092
.arg("-c")
9193
.arg(before_build)
9294
.current_dir(app_dir()),
93-
)?;
95+
)
96+
.with_context(|| format!("failed to run `{}` with `sh -c`", before_build))?;
9497
}
9598
}
9699

@@ -108,11 +111,13 @@ impl Build {
108111
.or(runner_from_config)
109112
.unwrap_or_else(|| "cargo".to_string());
110113

111-
rust::build_project(runner, &self.target, self.debug)?;
114+
rust::build_project(runner, &self.target, self.debug).with_context(|| "failed to build app")?;
112115

113116
let app_settings = rust::AppSettings::new(&config_)?;
114117

115-
let out_dir = app_settings.get_out_dir(self.debug)?;
118+
let out_dir = app_settings
119+
.get_out_dir(self.debug)
120+
.with_context(|| "failed to get project out directory")?;
116121
if let Some(product_name) = config_.package.product_name.clone() {
117122
let bin_name = app_settings.cargo_package_settings().name.clone();
118123
#[cfg(windows)]
@@ -176,9 +181,11 @@ impl Build {
176181
}
177182

178183
// Bundle the project
179-
let settings = settings_builder.build()?;
184+
let settings = settings_builder
185+
.build()
186+
.with_context(|| "failed to build bundler settings")?;
180187

181-
let bundles = bundle_project(settings)?;
188+
let bundles = bundle_project(settings).with_context(|| "failed to bundle project")?;
182189

183190
// If updater is active and pubkey is available
184191
if config_.tauri.updater.active && config_.tauri.updater.pubkey.is_some() {

tooling/cli.rs/src/build/rust.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
str::FromStr,
1111
};
1212

13+
use anyhow::Context;
1314
use serde::Deserialize;
1415

1516
use crate::helpers::{app_paths::tauri_dir, config::Config};
@@ -70,9 +71,13 @@ impl CargoSettings {
7071
fn load(dir: &Path) -> crate::Result<Self> {
7172
let toml_path = dir.join("Cargo.toml");
7273
let mut toml_str = String::new();
73-
let mut toml_file = File::open(toml_path)?;
74-
toml_file.read_to_string(&mut toml_str)?;
75-
toml::from_str(&toml_str).map_err(Into::into)
74+
let mut toml_file = File::open(toml_path).with_context(|| "failed to open Cargo.toml")?;
75+
toml_file
76+
.read_to_string(&mut toml_str)
77+
.with_context(|| "failed to read Cargo.toml")?;
78+
toml::from_str(&toml_str)
79+
.with_context(|| "failed to parse Cargo.toml")
80+
.map_err(Into::into)
7681
}
7782
}
7883

@@ -99,7 +104,10 @@ pub fn build_project(runner: String, target: &Option<String>, debug: bool) -> cr
99104
args.push("--release");
100105
}
101106

102-
let status = Command::new(&runner).args(args).status()?;
107+
let status = Command::new(&runner)
108+
.args(args)
109+
.status()
110+
.with_context(|| format!("failed to run {}", runner))?;
103111
if !status.success() {
104112
return Err(anyhow::anyhow!(format!(
105113
"Result of `{} build` operation was unsuccessful: {}",
@@ -118,7 +126,8 @@ pub struct AppSettings {
118126

119127
impl AppSettings {
120128
pub fn new(config: &Config) -> crate::Result<Self> {
121-
let cargo_settings = CargoSettings::load(&tauri_dir())?;
129+
let cargo_settings =
130+
CargoSettings::load(&tauri_dir()).with_context(|| "failed to load cargo settings")?;
122131
let cargo_package_settings = match &cargo_settings.package {
123132
Some(package_info) => package_info.clone(),
124133
None => {
@@ -268,9 +277,13 @@ fn get_target_dir(
268277
// if the path exists, parse it
269278
if cargo_config_path.exists() {
270279
let mut config_str = String::new();
271-
let mut config_file = File::open(cargo_config_path)?;
272-
config_file.read_to_string(&mut config_str)?;
273-
let config: CargoConfig = toml::from_str(&config_str)?;
280+
let mut config_file = File::open(&cargo_config_path)
281+
.with_context(|| format!("failed to open {:?}", cargo_config_path))?;
282+
config_file
283+
.read_to_string(&mut config_str)
284+
.with_context(|| "failed to read cargo config file")?;
285+
let config: CargoConfig =
286+
toml::from_str(&config_str).with_context(|| "failed to parse cargo config file")?;
274287
if let Some(build) = config.build {
275288
if let Some(target_dir) = build.target_dir {
276289
break Some(target_dir.into());

tooling/cli.rs/src/dev.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::helpers::{
99
Logger,
1010
};
1111

12+
use anyhow::Context;
1213
use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
1314
use once_cell::sync::OnceCell;
1415
use shared_child::SharedChild;
@@ -74,7 +75,7 @@ impl Dev {
7475
pub fn run(self) -> crate::Result<()> {
7576
let logger = Logger::new("tauri:dev");
7677
let tauri_path = tauri_dir();
77-
set_current_dir(&tauri_path)?;
78+
set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
7879
let merge_config = self.config.clone();
7980
let config = get_config(merge_config.as_deref())?;
8081
let mut process: Arc<SharedChild>;
@@ -94,13 +95,15 @@ impl Dev {
9495
.arg("/C")
9596
.arg(before_dev)
9697
.current_dir(app_dir())
97-
.spawn()?;
98+
.spawn()
99+
.with_context(|| format!("failed to run `{}` with `cmd /C`", before_dev))?;
98100
#[cfg(not(target_os = "windows"))]
99101
let child = Command::new("sh")
100102
.arg("-c")
101103
.arg(before_dev)
102104
.current_dir(app_dir())
103-
.spawn()?;
105+
.spawn()
106+
.with_context(|| format!("failed to run `{}` with `sh -c`", before_dev))?;
104107
BEFORE_DEV.set(Mutex::new(child)).unwrap();
105108
}
106109
}
@@ -173,7 +176,9 @@ impl Dev {
173176
// which will trigger the watcher again
174177
// So the app should only be started when a file other than tauri.conf.json is changed
175178
let _ = child_wait_tx.send(());
176-
process.kill()?;
179+
process
180+
.kill()
181+
.with_context(|| "failed to kill app process")?;
177182
process = self.start_app(&runner, child_wait_rx.clone());
178183
}
179184
}

tooling/cli.rs/src/helpers/config.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use anyhow::Context;
56
#[cfg(target_os = "linux")]
67
use heck::KebabCase;
78
use json_patch::merge;
@@ -52,7 +53,8 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
5253
let path = super::app_paths::tauri_dir().join("tauri.conf.json");
5354
let file = File::open(path)?;
5455
let buf = BufReader::new(file);
55-
let mut config: JsonValue = serde_json::from_reader(buf)?;
56+
let mut config: JsonValue =
57+
serde_json::from_reader(buf).with_context(|| "failed to parse `tauri.conf.json`")?;
5658

5759
let schema: JsonValue = serde_json::from_str(include_str!("../../schema.json"))?;
5860
let mut scope = valico::json_schema::Scope::new();
@@ -75,7 +77,8 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result<Confi
7577
}
7678

7779
if let Some(merge_config) = merge_config {
78-
let merge_config: JsonValue = serde_json::from_str(&merge_config)?;
80+
let merge_config: JsonValue =
81+
serde_json::from_str(&merge_config).with_context(|| "failed to parse config to merge")?;
7982
merge(&mut config, &merge_config);
8083
}
8184

tooling/cli.rs/src/helpers/manifest.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use super::{app_paths::tauri_dir, config::ConfigHandle};
66

7+
use anyhow::Context;
78
use toml_edit::{Array, Document, InlineTable, Item, Value};
89

910
use std::{
@@ -14,9 +15,12 @@ use std::{
1415
pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result<()> {
1516
let manifest_path = tauri_dir().join("Cargo.toml");
1617
let mut manifest_str = String::new();
17-
let mut manifest_file = File::open(&manifest_path)?;
18+
let mut manifest_file = File::open(&manifest_path)
19+
.with_context(|| format!("failed to open `{:?}` file", manifest_path))?;
1820
manifest_file.read_to_string(&mut manifest_str)?;
19-
let mut manifest: Document = manifest_str.parse::<Document>()?;
21+
let mut manifest: Document = manifest_str
22+
.parse::<Document>()
23+
.with_context(|| "failed to parse Cargo.toml")?;
2024
let dependencies = manifest
2125
.as_table_mut()
2226
.entry("dependencies")
@@ -68,7 +72,8 @@ pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result<()> {
6872
return Ok(());
6973
}
7074

71-
let mut manifest_file = File::create(&manifest_path)?;
75+
let mut manifest_file =
76+
File::create(&manifest_path).with_context(|| "failed to open Cargo.toml for rewrite")?;
7277
manifest_file.write_all(
7378
manifest
7479
.to_string_in_original_order()

tooling/cli.rs/src/init.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::{
1010
};
1111

1212
use crate::helpers::Logger;
13+
use anyhow::Context;
1314
use handlebars::{to_json, Handlebars};
1415
use include_dir::{include_dir, Dir};
1516
use serde::Deserialize;
@@ -142,7 +143,8 @@ impl Init {
142143
to_json(self.window_title.unwrap_or_else(|| "Tauri".to_string())),
143144
);
144145

145-
render_template(&handlebars, &data, &TEMPLATE_DIR, &self.directory)?;
146+
render_template(&handlebars, &data, &TEMPLATE_DIR, &self.directory)
147+
.with_context(|| "failed to render Tauri template")?;
146148
}
147149

148150
Ok(())

tooling/cli.rs/src/sign.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::helpers::updater_signature::{
77
};
88
use std::path::{Path, PathBuf};
99

10+
use anyhow::Context;
11+
1012
#[derive(Default)]
1113
pub struct Signer {
1214
private_key: Option<String>,
@@ -63,7 +65,8 @@ impl Signer {
6365
self.password.unwrap(),
6466
self.file.unwrap(),
6567
false,
66-
)?;
68+
)
69+
.with_context(|| "failed to sign file")?;
6770

6871
println!(
6972
"\nYour file was signed successfully, You can find the signature here:\n{}\n\nPublic signature:\n{}\n\nMake sure to include this into the signature field of your update server.",

0 commit comments

Comments
 (0)