mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-27 08:14:33 +08:00
85334d8dce
Backend improvements:
- Add InvalidHttpMethod error enum for better error semantics
- Clamp HTTP timeout to 2-30s to prevent config abuse
- Strict HTTP method validation instead of silent fallback to GET
Frontend improvements:
- Add i18n support for usage query errors (en/zh)
- Improve error handling with type-safe unknown instead of any
- Optimize i18n import (direct import instead of dynamic)
- Disable auto-retry for usage queries to avoid API stampede
Additional changes:
- Apply prettier formatting to affected files
Files changed:
- src-tauri/src/error.rs (+2)
- src-tauri/src/usage_script.rs (+8 -2)
- src/i18n/locales/{en,zh}.json (+4 -1 each)
- src/lib/api/usage.ts (+21 -4)
- src/lib/query/queries.ts (+1)
- style: prettier formatting on 6 other files
101 lines
2.3 KiB
Rust
101 lines
2.3 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("供应商不存在: {0}")]
|
|
ProviderNotFound(String),
|
|
#[error("MCP 校验失败: {0}")]
|
|
McpValidation(String),
|
|
#[error("{0}")]
|
|
Message(String),
|
|
#[error("不支持的 HTTP 方法: {0}")]
|
|
InvalidHttpMethod(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()
|
|
}
|
|
}
|