refine(usage): enhance query robustness and error handling
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
This commit is contained in:
@@ -1,12 +1,29 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import type { UsageResult } from "@/types";
|
||||
import type { AppId } from "./types";
|
||||
import i18n from "@/i18n";
|
||||
|
||||
export const usageApi = {
|
||||
async query(providerId: string, appId: AppId): Promise<UsageResult> {
|
||||
return await invoke("query_provider_usage", {
|
||||
provider_id: providerId,
|
||||
app: appId,
|
||||
});
|
||||
try {
|
||||
return await invoke("query_provider_usage", {
|
||||
provider_id: providerId,
|
||||
app: appId,
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
// 提取错误消息:优先使用后端返回的错误信息
|
||||
const message =
|
||||
typeof error === "string"
|
||||
? error
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: "";
|
||||
|
||||
// 如果没有错误消息,使用国际化的默认提示
|
||||
return {
|
||||
success: false,
|
||||
error: message || i18n.t("errors.usage_query_failed"),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user