chore: adjust error log

This commit is contained in:
GyDi 2022-03-12 15:57:16 +08:00
parent ec0fdf83b2
commit 4e806e21a6
No known key found for this signature in database
GPG Key ID: 58B15242BA8277A6

View File

@ -6,7 +6,7 @@ use crate::{
use crate::{ret_err, wrap_err}; use crate::{ret_err, wrap_err};
use anyhow::Result; use anyhow::Result;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::{path::PathBuf, process::Command, process::Stdio}; use std::{path::PathBuf, process::Command};
use tauri::{api, Manager, State}; use tauri::{api, Manager, State};
/// get all profiles from `profiles.yaml` /// get all profiles from `profiles.yaml`
@ -321,55 +321,53 @@ pub fn open_logs_dir() -> Result<(), String> {
open_path_cmd(log_dir, "failed to open logs dir") open_path_cmd(log_dir, "failed to open logs dir")
} }
/// get open/explorer command /// use the os default open command to open file or dir
fn open_path_cmd(dir: PathBuf, err_str: &str) -> Result<(), String> { fn open_path_cmd(path: PathBuf, err_str: &str) -> Result<(), String> {
let result; let result;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
use std::os::windows::process::CommandExt; use std::os::windows::process::CommandExt;
result = Command::new("explorer") result = Command::new("explorer")
.creation_flags(0x08000000) .creation_flags(0x08000000)
.arg(&dir) .arg(&path)
.spawn(); .spawn();
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
result = Command::new("open").arg(&dir).spawn(); result = Command::new("open").arg(&path).spawn();
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
result = Command::new("xdg-open").arg(&dir).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn(); result = Command::new("xdg-open").arg(&path).spawn();
} }
match result { match result {
Ok(child) => { Ok(child) => match child.wait_with_output() {
match child.wait_with_output() { Ok(out) => {
Ok(out) => { if let Some(code) = out.status.code() {
if let Some(code) = out.status.code() { if code != 0 {
if code != 0 { log::error!(
log::error!("open dir {:?} failed, child exit code: {:?}, stderr: {:?} stdout: {:?}", "failed to open path {:?} for {} (code {code})",
&dir, code, String::from_utf8_lossy(&out.stderr), String::from_utf8_lossy(&out.stdout)); &path,
return Err(err_str.into()); String::from_utf8_lossy(&out.stderr),
} );
return Err(err_str.into());
} }
},
Err(err) => {
log::error!("open dir {:?} failed, child exec err: {}", &dir, err);
return Err(err_str.into());
} }
} }
Err(err) => {
log::error!("failed to open path {:?} for {err}", &path);
return Err(err_str.into());
}
}, },
Err(err) => { Err(err) => {
log::error!("open dir {:?} spawn process failed, err: {}", &dir, err); log::error!("failed to open path {:?} for {err}", &path);
return Err(err_str.into()); return Err(err_str.into());
}, }
_ => {
log::error!("open dir {:?} failed due to OS not supported", &dir);
return Err(err_str.into());
},
} }
return Ok(()); return Ok(());