fix: improve error handling and logging in various modules
This commit is contained in:
@@ -24,6 +24,8 @@ use futures::future::join_all;
|
||||
use parking_lot::Mutex;
|
||||
use smartstring::alias::String;
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::{
|
||||
sync::atomic::{AtomicBool, Ordering},
|
||||
@@ -575,9 +577,6 @@ impl Tray {
|
||||
return;
|
||||
}
|
||||
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
let fut: Pin<Box<dyn Future<Output = ()> + Send>> = match tray_event.as_str() {
|
||||
"system_proxy" => Box::pin(async move {
|
||||
feat::toggle_system_proxy().await;
|
||||
@@ -1226,7 +1225,14 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
||||
};
|
||||
feat::switch_proxy_node(group_name, proxy_name).await;
|
||||
}
|
||||
_ => {}
|
||||
_ => {
|
||||
logging!(
|
||||
debug,
|
||||
Type::Tray,
|
||||
"Unhandled tray menu event: {:?}",
|
||||
event.id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// We dont expected to refresh tray state here
|
||||
|
||||
@@ -275,41 +275,43 @@ impl CoreConfigValidator {
|
||||
logging!(info, Type::Validate, "验证目录: {}", app_dir_str);
|
||||
|
||||
// 使用子进程运行clash验证配置
|
||||
let output = app_handle
|
||||
.shell()
|
||||
.sidecar(clash_core.as_str())?
|
||||
.args(["-t", "-d", app_dir_str, "-f", config_path])
|
||||
.output()
|
||||
.await?;
|
||||
let command = app_handle.shell().sidecar(clash_core.as_str())?.args([
|
||||
"-t",
|
||||
"-d",
|
||||
app_dir_str,
|
||||
"-f",
|
||||
config_path,
|
||||
]);
|
||||
let output = command.output().await?;
|
||||
|
||||
let stderr = std::string::String::from_utf8_lossy(&output.stderr);
|
||||
let stdout = std::string::String::from_utf8_lossy(&output.stdout);
|
||||
let status = &output.status;
|
||||
let stderr = &output.stderr;
|
||||
let stdout = &output.stdout;
|
||||
|
||||
// 检查进程退出状态和错误输出
|
||||
let error_keywords = ["FATA", "fatal", "Parse config error", "level=fatal"];
|
||||
let has_error =
|
||||
!output.status.success() || error_keywords.iter().any(|&kw| stderr.contains(kw));
|
||||
let has_error = !status.success() || contains_any_keyword(stderr, &error_keywords);
|
||||
|
||||
logging!(info, Type::Validate, "-------- 验证结果 --------");
|
||||
|
||||
if !stderr.is_empty() {
|
||||
logging!(info, Type::Validate, "stderr输出:\n{}", stderr);
|
||||
logging!(info, Type::Validate, "stderr输出:\n{:?}", stderr);
|
||||
}
|
||||
|
||||
if has_error {
|
||||
logging!(info, Type::Validate, "发现错误,开始处理错误信息");
|
||||
let error_msg = if !stdout.is_empty() {
|
||||
stdout.into()
|
||||
let error_msg: String = if !stdout.is_empty() {
|
||||
str::from_utf8(stdout).unwrap_or_default().into()
|
||||
} else if !stderr.is_empty() {
|
||||
stderr.into()
|
||||
} else if let Some(code) = output.status.code() {
|
||||
format!("验证进程异常退出,退出码: {code}")
|
||||
str::from_utf8(stderr).unwrap_or_default().into()
|
||||
} else if let Some(code) = status.code() {
|
||||
format!("验证进程异常退出,退出码: {code}").into()
|
||||
} else {
|
||||
"验证进程被终止".into()
|
||||
};
|
||||
|
||||
logging!(info, Type::Validate, "-------- 验证结束 --------");
|
||||
Ok((false, error_msg.into())) // 返回错误消息给调用者处理
|
||||
Ok((false, error_msg)) // 返回错误消息给调用者处理
|
||||
} else {
|
||||
logging!(info, Type::Validate, "验证成功");
|
||||
logging!(info, Type::Validate, "-------- 验证结束 --------");
|
||||
@@ -342,6 +344,23 @@ fn has_ext<P: AsRef<std::path::Path>>(path: P, ext: &str) -> bool {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn contains_any_keyword<'a>(buf: &'a [u8], keywords: &'a [&str]) -> bool {
|
||||
for &kw in keywords {
|
||||
let needle = kw.as_bytes();
|
||||
if needle.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut i = 0;
|
||||
while i + needle.len() <= buf.len() {
|
||||
if &buf[i..i + needle.len()] == needle {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
singleton_lazy!(
|
||||
CoreConfigValidator,
|
||||
CORECONFIGVALIDATOR,
|
||||
|
||||
Reference in New Issue
Block a user