Files
cc-switch/src-tauri/src/error.rs
Jason b498f0fe91 refactor(i18n): complete error message internationalization and code cleanup
Replaced all remaining hardcoded error types with AppError::localized for
full bilingual support and simplified error handling logic.

Backend changes:
- usage_script.rs: Converted InvalidHttpMethod to localized error
- provider.rs: Replaced all 7 ProviderNotFound instances with localized errors
  * Line 436: Delete provider validation
  * Line 625: Update provider metadata
  * Line 785: Test usage script provider lookup
  * Line 855: Query usage provider lookup
  * Line 924: Prepare Codex provider switch
  * Line 1011: Prepare Claude provider switch
  * Line 1272: Delete provider snapshot
- provider.rs: Simplified error message formatting (removed 40+ lines)
  * Removed redundant string matching fallback logic
  * Now uses clean language-based selection for Localized errors
  * Falls back to default Display for other error types
- error.rs: Removed unused error variants
  * Deleted InvalidHttpMethod (replaced with localized)
  * Deleted ProviderNotFound (replaced with localized)

Code quality improvements:
- Reduced complexity: 40+ lines of string matching removed
- Better maintainability: Centralized error message handling
- Type safety: All provider errors now use consistent localized format

Impact:
- 100% i18n coverage for provider and usage script error messages
- Cleaner, more maintainable error handling code
- No unused error variants remaining
2025-11-05 10:11:47 +08:00

97 lines
2.2 KiB
Rust

use std::path::Path;
use std::sync::PoisonError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum AppError {
#[error("配置错误: {0}")]
Config(String),
#[error("无效输入: {0}")]
InvalidInput(String),
#[error("IO 错误: {path}: {source}")]
Io {
path: String,
#[source]
source: std::io::Error,
},
#[error("{context}: {source}")]
IoContext {
context: String,
#[source]
source: std::io::Error,
},
#[error("JSON 解析错误: {path}: {source}")]
Json {
path: String,
#[source]
source: serde_json::Error,
},
#[error("JSON 序列化失败: {source}")]
JsonSerialize {
#[source]
source: serde_json::Error,
},
#[error("TOML 解析错误: {path}: {source}")]
Toml {
path: String,
#[source]
source: toml::de::Error,
},
#[error("锁获取失败: {0}")]
Lock(String),
#[error("MCP 校验失败: {0}")]
McpValidation(String),
#[error("{0}")]
Message(String),
#[error("{zh} ({en})")]
Localized {
key: &'static str,
zh: String,
en: String,
},
}
impl AppError {
pub fn io(path: impl AsRef<Path>, source: std::io::Error) -> Self {
Self::Io {
path: path.as_ref().display().to_string(),
source,
}
}
pub fn json(path: impl AsRef<Path>, source: serde_json::Error) -> Self {
Self::Json {
path: path.as_ref().display().to_string(),
source,
}
}
pub fn toml(path: impl AsRef<Path>, source: toml::de::Error) -> Self {
Self::Toml {
path: path.as_ref().display().to_string(),
source,
}
}
pub fn localized(key: &'static str, zh: impl Into<String>, en: impl Into<String>) -> Self {
Self::Localized {
key,
zh: zh.into(),
en: en.into(),
}
}
}
impl<T> From<PoisonError<T>> for AppError {
fn from(err: PoisonError<T>) -> Self {
Self::Lock(err.to_string())
}
}
impl From<AppError> for String {
fn from(err: AppError) -> Self {
err.to_string()
}
}