refactor(logging): replace log_err! with structured logging_error! calls

refactor(cm-service): better error handling from Backend to Frontend
This commit is contained in:
Tunglies
2025-03-28 03:39:21 +08:00
parent 59caa22431
commit e8e16f7d57
18 changed files with 232 additions and 128 deletions

View File

@@ -200,7 +200,7 @@ pub fn copy_icon_file(path: String, icon_info: IconInfo) -> CmdResult<String> {
}
logging!(
info,
Type::CMD,
Type::Cmd,
true,
"Copying icon file path: {:?} -> file dist: {:?}",
path,

View File

@@ -1,8 +1,8 @@
use super::CmdResult;
use crate::{
config::*,
core::*,
feat, log_err, logging, ret_err,
core::{tray::Tray, *},
feat, logging, logging_error, ret_err,
utils::{dirs, help, logging::Type},
wrap_err,
};
@@ -19,8 +19,8 @@ pub fn get_profiles() -> CmdResult<IProfiles> {
pub async fn enhance_profiles() -> CmdResult {
match CoreManager::global().update_config().await {
Ok((true, _)) => {
println!("[enhance_profiles] 配置更新成功");
log_err!(tray::Tray::global().update_tooltip());
logging!(info, Type::Cmd, true, "配置更新成功");
logging_error!(Type::Tray, true, Tray::global().update_tooltip());
handle::Handle::refresh_clash();
Ok(())
}
@@ -77,19 +77,19 @@ pub async fn delete_profile(index: String) -> CmdResult {
/// 修改profiles的配置
#[tauri::command]
pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
logging!(info, Type::CMD, true, "开始修改配置文件");
logging!(info, Type::Cmd, true, "开始修改配置文件");
// 保存当前配置,以便在验证失败时恢复
let current_profile = Config::profiles().latest().current.clone();
logging!(info, Type::CMD, true, "当前配置: {:?}", current_profile);
logging!(info, Type::Cmd, true, "当前配置: {:?}", current_profile);
// 更新profiles配置
logging!(info, Type::CMD, true, "正在更新配置草稿");
logging!(info, Type::Cmd, true, "正在更新配置草稿");
wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
// 更新配置并进行验证
match CoreManager::global().update_config().await {
Ok((true, _)) => {
logging!(info, Type::CMD, true, "配置更新成功");
logging!(info, Type::Cmd, true, "配置更新成功");
handle::Handle::refresh_clash();
let _ = tray::Tray::global().update_tooltip();
Config::profiles().apply();
@@ -97,13 +97,13 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
Ok(true)
}
Ok((false, error_msg)) => {
logging!(warn, Type::CMD, true, "配置验证失败: {}", error_msg);
logging!(warn, Type::Cmd, true, "配置验证失败: {}", error_msg);
Config::profiles().discard();
// 如果验证失败,恢复到之前的配置
if let Some(prev_profile) = current_profile {
logging!(
info,
Type::CMD,
Type::Cmd,
true,
"尝试恢复到之前的配置: {}",
prev_profile
@@ -116,7 +116,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
wrap_err!({ Config::profiles().draft().patch_config(restore_profiles) })?;
Config::profiles().apply();
wrap_err!(Config::profiles().data().save_file())?;
logging!(info, Type::CMD, true, "成功恢复到之前的配置");
logging!(info, Type::Cmd, true, "成功恢复到之前的配置");
}
// 发送验证错误通知
@@ -124,7 +124,7 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
Ok(false)
}
Err(e) => {
logging!(warn, Type::CMD, true, "更新过程发生错误: {}", e);
logging!(warn, Type::Cmd, true, "更新过程发生错误: {}", e);
Config::profiles().discard();
handle::Handle::notice_message("config_validate::boot_error", e.to_string());
Ok(false)

View File

@@ -5,34 +5,39 @@ use crate::{
utils::logging::Type,
};
async fn execute_service_operation(
service_op: impl std::future::Future<Output = Result<(), impl ToString + std::fmt::Debug>>,
op_type: &str,
) -> CmdResult {
if let Err(e) = service_op.await {
let emsg = format!("{} {} failed: {:?}", op_type, "service", e);
logging_error!(Type::Service, true, "{}", emsg);
return Err(emsg);
}
if let Err(e) = CoreManager::global().restart_core().await {
let emsg = format!("{} {} failed: {:?}", op_type, "core", e);
logging_error!(Type::Core, true, "{}", emsg);
return Err(emsg);
}
Ok(())
}
#[tauri::command]
pub async fn install_service() -> CmdResult {
logging_error!(Type::Service, true, service::install_service().await);
logging_error!(Type::Core, true, CoreManager::global().restart_core().await);
Ok(())
execute_service_operation(service::install_service(), "Install").await
}
#[tauri::command]
pub async fn uninstall_service() -> CmdResult {
logging_error!(Type::Service, true, service::uninstall_service().await);
logging_error!(Type::Core, true, CoreManager::global().restart_core().await);
Ok(())
execute_service_operation(service::uninstall_service(), "Uninstall").await
}
#[tauri::command]
pub async fn reinstall_service() -> CmdResult {
logging_error!(Type::Service, true, service::reinstall_service().await);
logging_error!(Type::Core, true, CoreManager::global().restart_core().await);
Ok(())
execute_service_operation(service::reinstall_service(), "Reinstall").await
}
#[tauri::command]
pub async fn repair_service() -> CmdResult {
logging_error!(
Type::Service,
true,
service::force_reinstall_service().await
);
logging_error!(Type::Core, true, CoreManager::global().restart_core().await);
Ok(())
execute_service_operation(service::force_reinstall_service(), "Repair").await
}