refactor: improve error handling and code formatting

- Enhanced error messages in Rust backend to include file paths
- Improved provider switching error handling with detailed messages
- Added MCP button placeholder in UI (functionality TODO)
- Applied code formatting across frontend components
- Extended error notification duration to 6s for better readability
This commit is contained in:
Jason
2025-10-08 21:22:56 +08:00
parent 6afc436946
commit e9833e9a57
20 changed files with 335 additions and 237 deletions

View File

@@ -60,17 +60,20 @@ pub fn write_codex_live_atomic(auth: &Value, config_text_opt: Option<&str>) -> R
let config_path = get_codex_config_path();
if let Some(parent) = auth_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| format!("创建 Codex 目录失败: {}", e))?;
std::fs::create_dir_all(parent)
.map_err(|e| format!("创建 Codex 目录失败: {}: {}", parent.display(), e))?;
}
// 读取旧内容用于回滚
let old_auth = if auth_path.exists() {
Some(fs::read(&auth_path).map_err(|e| format!("读取旧 auth.json 失败: {}", e))?)
Some(fs::read(&auth_path)
.map_err(|e| format!("读取旧 auth.json 失败: {}: {}", auth_path.display(), e))?)
} else {
None
};
let _old_config = if config_path.exists() {
Some(fs::read(&config_path).map_err(|e| format!("读取旧 config.toml 失败: {}", e))?)
Some(fs::read(&config_path)
.map_err(|e| format!("读取旧 config.toml 失败: {}: {}", config_path.display(), e))?)
} else {
None
};
@@ -81,8 +84,13 @@ pub fn write_codex_live_atomic(auth: &Value, config_text_opt: Option<&str>) -> R
None => String::new(),
};
if !cfg_text.trim().is_empty() {
toml::from_str::<toml::Table>(&cfg_text)
.map_err(|e| format!("config.toml 格式错误: {}", e))?;
toml::from_str::<toml::Table>(&cfg_text).map_err(|e| {
format!(
"config.toml 语法错误: {} (路径: {})",
e,
config_path.display()
)
})?;
}
// 第一步:写 auth.json

View File

@@ -336,8 +336,13 @@ pub async fn switch_provider(
if auth_path.exists() {
let auth: Value = crate::config::read_json_file(&auth_path)?;
let config_str = if config_path.exists() {
std::fs::read_to_string(&config_path)
.map_err(|e| format!("读取 config.toml 失败: {}", e))?
std::fs::read_to_string(&config_path).map_err(|e| {
format!(
"读取 config.toml 失败: {}: {}",
config_path.display(),
e
)
})?
} else {
String::new()
};

View File

@@ -118,16 +118,19 @@ pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, Stri
return Err(format!("文件不存在: {}", path.display()));
}
let content = fs::read_to_string(path).map_err(|e| format!("读取文件失败: {}", e))?;
let content = fs::read_to_string(path)
.map_err(|e| format!("读取文件失败: {}: {}", path.display(), e))?;
serde_json::from_str(&content).map_err(|e| format!("解析 JSON 失败: {}", e))
serde_json::from_str(&content)
.map_err(|e| format!("解析 JSON 失败: {}: {}", path.display(), e))
}
/// 写入 JSON 配置文件
pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), String> {
// 确保目录存在
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
fs::create_dir_all(parent)
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
}
let json =
@@ -139,7 +142,8 @@ pub fn write_json_file<T: Serialize>(path: &Path, data: &T) -> Result<(), String
/// 原子写入文本文件(用于 TOML/纯文本)
pub fn write_text_file(path: &Path, data: &str) -> Result<(), String> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
fs::create_dir_all(parent)
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
}
atomic_write(path, data.as_bytes())
}
@@ -147,7 +151,8 @@ pub fn write_text_file(path: &Path, data: &str) -> Result<(), String> {
/// 原子写入:写入临时文件后 rename 替换,避免半写状态
pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| format!("创建目录失败: {}", e))?;
fs::create_dir_all(parent)
.map_err(|e| format!("创建目录失败: {}: {}", parent.display(), e))?;
}
let parent = path.parent().ok_or_else(|| "无效的路径".to_string())?;
@@ -164,10 +169,12 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
tmp.push(format!("{}.tmp.{}", file_name, ts));
{
let mut f = fs::File::create(&tmp).map_err(|e| format!("创建临时文件失败: {}", e))?;
let mut f = fs::File::create(&tmp)
.map_err(|e| format!("创建临时文件失败: {}: {}", tmp.display(), e))?;
f.write_all(data)
.map_err(|e| format!("写入临时文件失败: {}", e))?;
f.flush().map_err(|e| format!("刷新临时文件失败: {}", e))?;
.map_err(|e| format!("写入临时文件失败: {}: {}", tmp.display(), e))?;
f.flush()
.map_err(|e| format!("刷新临时文件失败: {}: {}", tmp.display(), e))?;
}
#[cfg(unix)]
@@ -185,12 +192,14 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
if path.exists() {
let _ = fs::remove_file(path);
}
fs::rename(&tmp, path).map_err(|e| format!("原子替换失败: {}", e))?;
fs::rename(&tmp, path)
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
}
#[cfg(not(windows))]
{
fs::rename(&tmp, path).map_err(|e| format!("原子替换失败: {}", e))?;
fs::rename(&tmp, path)
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
}
Ok(())
}