feat: add error prompt for loading initial config file

This commit is contained in:
wonfen 2025-03-29 07:52:46 +08:00
parent d54a765bd6
commit a9cccc7b97
3 changed files with 93 additions and 15 deletions

View File

@ -16,6 +16,7 @@
#### 新增了:
- Clash Verge Rev 从现在开始不再强依赖系统服务和管理权限
- 支持根据用户偏好选择Sidecar(用户空间)模式或安装服务
- 增加载入初始配置文件的错误提示
#### 优化了:
- 重构了后端内核管理逻辑,更轻量化和有效的管理内核,提高了性能和稳定性

View File

@ -1,10 +1,11 @@
use super::{prfitem::PrfItem, PrfOption};
use crate::utils::{dirs, help};
use crate::{logging, utils::logging::Type};
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use serde_yaml::Mapping;
use serde_yaml::Value;
use std::{fs, io::Write};
/// Define the `profiles.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct IProfiles {
@ -382,16 +383,92 @@ impl IProfiles {
pub fn current_mapping(&self) -> Result<Mapping> {
match (self.current.as_ref(), self.items.as_ref()) {
(Some(current), Some(items)) => {
logging!(
info,
Type::Config,
true,
"开始获取当前配置文件 current_uid={}",
current
);
logging!(info, Type::Core, true, "服务可用,直接使用服务模式");
if let Some(item) = items.iter().find(|e| e.uid.as_ref() == Some(current)) {
let file_path = match item.file.as_ref() {
Some(file) => dirs::app_profiles_dir()?.join(file),
None => bail!("failed to get the file field"),
};
return help::read_mapping(&file_path);
Some(file) => {
let path = dirs::app_profiles_dir()?.join(file);
logging!(
info,
Type::Config,
true,
"找到配置文件路径: {}",
path.display()
);
path
}
None => {
logging!(
error,
Type::Config,
true,
"配置项缺少file字段 uid={}",
current
);
bail!("failed to get the file field");
}
};
if !file_path.exists() {
logging!(
error,
Type::Config,
true,
"配置文件不存在: {}",
file_path.display()
);
}
match help::read_mapping(&file_path) {
Ok(mapping) => {
let key_count = mapping.len();
logging!(
info,
Type::Config,
true,
"成功读取配置文件, 包含{}个键值对",
key_count
);
// 打印主要的配置键
let important_keys = ["proxies", "proxy-groups", "rules"];
for key in important_keys.iter() {
if mapping.contains_key(&Value::from(*key)) {
logging!(info, Type::Config, true, "配置包含关键字段: {}", key);
} else {
logging!(warn, Type::Config, true, "配置缺少关键字段: {}", key);
}
}
return Ok(mapping);
}
Err(e) => {
logging!(error, Type::Config, true, "读取配置文件失败: {}", e);
// 将错误发送到前端显示
crate::core::handle::Handle::notice_message(
"config_validate::yaml_syntax_error",
&format!("{}", e),
);
return Err(e);
}
}
}
logging!(
error,
Type::Config,
true,
"未找到当前配置项 uid={}",
current
);
bail!("failed to find the current profile \"uid:{current}\"");
}
_ => Ok(Mapping::new()),
_ => {
logging!(warn, Type::Config, true, "没有当前配置项,返回空配置");
Ok(Mapping::new())
}
}
}

View File

@ -13,13 +13,13 @@ use crate::{
const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task";
pub fn enable_auto_light_weight_mode() {
logging!(info, Type::Lightweight, True, "开启自动轻量模式");
logging!(info, Type::Lightweight, true, "开启自动轻量模式");
setup_window_close_listener();
setup_webview_focus_listener();
}
pub fn disable_auto_light_weight_mode() {
logging!(info, Type::Lightweight, True, "关闭自动轻量模式");
logging!(info, Type::Lightweight, true, "关闭自动轻量模式");
let _ = cancel_light_weight_timer();
cancel_window_close_listener();
}
@ -34,7 +34,7 @@ pub fn entry_lightweight_mode() {
}
#[cfg(target_os = "macos")]
AppHandleManager::global().set_activation_policy_accessory();
logging!(info, Type::Lightweight, True, "轻量模式已开启");
logging!(info, Type::Lightweight, true, "轻量模式已开启");
}
let _ = cancel_light_weight_timer();
}
@ -46,7 +46,7 @@ fn setup_window_close_listener() -> u32 {
logging!(
info,
Type::Lightweight,
True,
true,
"监听到关闭请求,开始轻量模式计时"
);
});
@ -62,7 +62,7 @@ fn setup_webview_focus_listener() -> u32 {
logging!(
info,
Type::Lightweight,
True,
true,
"监听到窗口获得焦点,取消轻量模式计时"
);
});
@ -74,7 +74,7 @@ fn setup_webview_focus_listener() -> u32 {
fn cancel_window_close_listener() {
if let Some(window) = handle::Handle::global().get_window() {
window.unlisten(setup_window_close_listener());
logging!(info, Type::Lightweight, True, "取消了窗口关闭监听");
logging!(info, Type::Lightweight, true, "取消了窗口关闭监听");
}
}
@ -98,7 +98,7 @@ fn setup_light_weight_timer() -> Result<()> {
.set_maximum_parallel_runnable_num(1)
.set_frequency_once_by_minutes(once_by_minutes)
.spawn_async_routine(move || async move {
logging!(info, Type::Timer, True, "计时器到期,开始进入轻量模式");
logging!(info, Type::Timer, true, "计时器到期,开始进入轻量模式");
entry_lightweight_mode();
})
.context("failed to create timer task")?;
@ -118,7 +118,7 @@ fn setup_light_weight_timer() -> Result<()> {
logging!(
info,
Type::Timer,
True,
true,
"计时器已设置,{} 分钟后将自动进入轻量模式",
once_by_minutes
);
@ -134,7 +134,7 @@ fn cancel_light_weight_timer() -> Result<()> {
delay_timer
.remove_task(task.task_id)
.context("failed to remove timer task")?;
logging!(info, Type::Timer, True, "计时器已取消");
logging!(info, Type::Timer, true, "计时器已取消");
}
Ok(())