From 5cc4b11f5d00a1e7e580e31785b31c491c06d8d7 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 1 May 2021 22:02:45 -0300 Subject: [PATCH] feat(cli.rs): add context to errors (#1674) --- .changes/cli-error-logging.md | 5 +++++ tooling/cli.rs/src/build.rs | 21 ++++++++++++------- tooling/cli.rs/src/build/rust.rs | 29 +++++++++++++++++++------- tooling/cli.rs/src/dev.rs | 13 ++++++++---- tooling/cli.rs/src/helpers/config.rs | 7 +++++-- tooling/cli.rs/src/helpers/manifest.rs | 11 +++++++--- tooling/cli.rs/src/init.rs | 4 +++- tooling/cli.rs/src/sign.rs | 5 ++++- 8 files changed, 69 insertions(+), 26 deletions(-) create mode 100644 .changes/cli-error-logging.md diff --git a/.changes/cli-error-logging.md b/.changes/cli-error-logging.md new file mode 100644 index 000000000..a4d885a7e --- /dev/null +++ b/.changes/cli-error-logging.md @@ -0,0 +1,5 @@ +--- +"cli.rs": patch +--- + +Improve error logging. diff --git a/tooling/cli.rs/src/build.rs b/tooling/cli.rs/src/build.rs index cad222241..4254ec69c 100644 --- a/tooling/cli.rs/src/build.rs +++ b/tooling/cli.rs/src/build.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +use anyhow::Context; use tauri_bundler::bundle::{bundle_project, PackageType, SettingsBuilder}; use crate::helpers::{ @@ -67,7 +68,7 @@ impl Build { let config = get_config(self.config.as_deref())?; let tauri_path = tauri_dir(); - set_current_dir(&tauri_path)?; + set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?; rewrite_manifest(config.clone())?; @@ -83,14 +84,16 @@ impl Build { .arg("/C") .arg(before_build) .current_dir(app_dir()), - )?; + ) + .with_context(|| format!("failed to run `{}` with `cmd /C`", before_build))?; #[cfg(not(target_os = "windows"))] execute_with_output( &mut Command::new("sh") .arg("-c") .arg(before_build) .current_dir(app_dir()), - )?; + ) + .with_context(|| format!("failed to run `{}` with `sh -c`", before_build))?; } } @@ -108,11 +111,13 @@ impl Build { .or(runner_from_config) .unwrap_or_else(|| "cargo".to_string()); - rust::build_project(runner, &self.target, self.debug)?; + rust::build_project(runner, &self.target, self.debug).with_context(|| "failed to build app")?; let app_settings = rust::AppSettings::new(&config_)?; - let out_dir = app_settings.get_out_dir(self.debug)?; + let out_dir = app_settings + .get_out_dir(self.debug) + .with_context(|| "failed to get project out directory")?; if let Some(product_name) = config_.package.product_name.clone() { let bin_name = app_settings.cargo_package_settings().name.clone(); #[cfg(windows)] @@ -176,9 +181,11 @@ impl Build { } // Bundle the project - let settings = settings_builder.build()?; + let settings = settings_builder + .build() + .with_context(|| "failed to build bundler settings")?; - let bundles = bundle_project(settings)?; + let bundles = bundle_project(settings).with_context(|| "failed to bundle project")?; // If updater is active and pubkey is available if config_.tauri.updater.active && config_.tauri.updater.pubkey.is_some() { diff --git a/tooling/cli.rs/src/build/rust.rs b/tooling/cli.rs/src/build/rust.rs index d14ba29d3..3a2e37de3 100644 --- a/tooling/cli.rs/src/build/rust.rs +++ b/tooling/cli.rs/src/build/rust.rs @@ -10,6 +10,7 @@ use std::{ str::FromStr, }; +use anyhow::Context; use serde::Deserialize; use crate::helpers::{app_paths::tauri_dir, config::Config}; @@ -70,9 +71,13 @@ impl CargoSettings { fn load(dir: &Path) -> crate::Result { let toml_path = dir.join("Cargo.toml"); let mut toml_str = String::new(); - let mut toml_file = File::open(toml_path)?; - toml_file.read_to_string(&mut toml_str)?; - toml::from_str(&toml_str).map_err(Into::into) + let mut toml_file = File::open(toml_path).with_context(|| "failed to open Cargo.toml")?; + toml_file + .read_to_string(&mut toml_str) + .with_context(|| "failed to read Cargo.toml")?; + toml::from_str(&toml_str) + .with_context(|| "failed to parse Cargo.toml") + .map_err(Into::into) } } @@ -99,7 +104,10 @@ pub fn build_project(runner: String, target: &Option, debug: bool) -> cr args.push("--release"); } - let status = Command::new(&runner).args(args).status()?; + let status = Command::new(&runner) + .args(args) + .status() + .with_context(|| format!("failed to run {}", runner))?; if !status.success() { return Err(anyhow::anyhow!(format!( "Result of `{} build` operation was unsuccessful: {}", @@ -118,7 +126,8 @@ pub struct AppSettings { impl AppSettings { pub fn new(config: &Config) -> crate::Result { - let cargo_settings = CargoSettings::load(&tauri_dir())?; + let cargo_settings = + CargoSettings::load(&tauri_dir()).with_context(|| "failed to load cargo settings")?; let cargo_package_settings = match &cargo_settings.package { Some(package_info) => package_info.clone(), None => { @@ -268,9 +277,13 @@ fn get_target_dir( // if the path exists, parse it if cargo_config_path.exists() { let mut config_str = String::new(); - let mut config_file = File::open(cargo_config_path)?; - config_file.read_to_string(&mut config_str)?; - let config: CargoConfig = toml::from_str(&config_str)?; + let mut config_file = File::open(&cargo_config_path) + .with_context(|| format!("failed to open {:?}", cargo_config_path))?; + config_file + .read_to_string(&mut config_str) + .with_context(|| "failed to read cargo config file")?; + let config: CargoConfig = + toml::from_str(&config_str).with_context(|| "failed to parse cargo config file")?; if let Some(build) = config.build { if let Some(target_dir) = build.target_dir { break Some(target_dir.into()); diff --git a/tooling/cli.rs/src/dev.rs b/tooling/cli.rs/src/dev.rs index f3f5efd8d..d547e3b6a 100644 --- a/tooling/cli.rs/src/dev.rs +++ b/tooling/cli.rs/src/dev.rs @@ -9,6 +9,7 @@ use crate::helpers::{ Logger, }; +use anyhow::Context; use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher}; use once_cell::sync::OnceCell; use shared_child::SharedChild; @@ -74,7 +75,7 @@ impl Dev { pub fn run(self) -> crate::Result<()> { let logger = Logger::new("tauri:dev"); let tauri_path = tauri_dir(); - set_current_dir(&tauri_path)?; + set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?; let merge_config = self.config.clone(); let config = get_config(merge_config.as_deref())?; let mut process: Arc; @@ -94,13 +95,15 @@ impl Dev { .arg("/C") .arg(before_dev) .current_dir(app_dir()) - .spawn()?; + .spawn() + .with_context(|| format!("failed to run `{}` with `cmd /C`", before_dev))?; #[cfg(not(target_os = "windows"))] let child = Command::new("sh") .arg("-c") .arg(before_dev) .current_dir(app_dir()) - .spawn()?; + .spawn() + .with_context(|| format!("failed to run `{}` with `sh -c`", before_dev))?; BEFORE_DEV.set(Mutex::new(child)).unwrap(); } } @@ -173,7 +176,9 @@ impl Dev { // which will trigger the watcher again // So the app should only be started when a file other than tauri.conf.json is changed let _ = child_wait_tx.send(()); - process.kill()?; + process + .kill() + .with_context(|| "failed to kill app process")?; process = self.start_app(&runner, child_wait_rx.clone()); } } diff --git a/tooling/cli.rs/src/helpers/config.rs b/tooling/cli.rs/src/helpers/config.rs index 2d876270d..a96b6c113 100644 --- a/tooling/cli.rs/src/helpers/config.rs +++ b/tooling/cli.rs/src/helpers/config.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +use anyhow::Context; #[cfg(target_os = "linux")] use heck::KebabCase; use json_patch::merge; @@ -52,7 +53,8 @@ fn get_internal(merge_config: Option<&str>, reload: bool) -> crate::Result, reload: bool) -> crate::Result crate::Result<()> { let manifest_path = tauri_dir().join("Cargo.toml"); let mut manifest_str = String::new(); - let mut manifest_file = File::open(&manifest_path)?; + let mut manifest_file = File::open(&manifest_path) + .with_context(|| format!("failed to open `{:?}` file", manifest_path))?; manifest_file.read_to_string(&mut manifest_str)?; - let mut manifest: Document = manifest_str.parse::()?; + let mut manifest: Document = manifest_str + .parse::() + .with_context(|| "failed to parse Cargo.toml")?; let dependencies = manifest .as_table_mut() .entry("dependencies") @@ -68,7 +72,8 @@ pub fn rewrite_manifest(config: ConfigHandle) -> crate::Result<()> { return Ok(()); } - let mut manifest_file = File::create(&manifest_path)?; + let mut manifest_file = + File::create(&manifest_path).with_context(|| "failed to open Cargo.toml for rewrite")?; manifest_file.write_all( manifest .to_string_in_original_order() diff --git a/tooling/cli.rs/src/init.rs b/tooling/cli.rs/src/init.rs index 973f3e261..b7b7afaff 100644 --- a/tooling/cli.rs/src/init.rs +++ b/tooling/cli.rs/src/init.rs @@ -10,6 +10,7 @@ use std::{ }; use crate::helpers::Logger; +use anyhow::Context; use handlebars::{to_json, Handlebars}; use include_dir::{include_dir, Dir}; use serde::Deserialize; @@ -142,7 +143,8 @@ impl Init { to_json(self.window_title.unwrap_or_else(|| "Tauri".to_string())), ); - render_template(&handlebars, &data, &TEMPLATE_DIR, &self.directory)?; + render_template(&handlebars, &data, &TEMPLATE_DIR, &self.directory) + .with_context(|| "failed to render Tauri template")?; } Ok(()) diff --git a/tooling/cli.rs/src/sign.rs b/tooling/cli.rs/src/sign.rs index 21b04f907..89ad91074 100644 --- a/tooling/cli.rs/src/sign.rs +++ b/tooling/cli.rs/src/sign.rs @@ -7,6 +7,8 @@ use crate::helpers::updater_signature::{ }; use std::path::{Path, PathBuf}; +use anyhow::Context; + #[derive(Default)] pub struct Signer { private_key: Option, @@ -63,7 +65,8 @@ impl Signer { self.password.unwrap(), self.file.unwrap(), false, - )?; + ) + .with_context(|| "failed to sign file")?; println!( "\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.",