fix(mcp): improve error message internationalization

- Add translateMcpBackendError utility to map backend errors to i18n keys
- Update error handling in McpPanel, McpFormModal, and McpWizardModal
- Internationalize stdio/http type selectors in Wizard
- Implement three-tier fallback strategy: translation → raw error → default message
- No backend changes required, fully frontend-based i18n implementation
This commit is contained in:
Jason
2025-10-11 16:20:12 +08:00
parent c2b27a4949
commit ea7080a42e
7 changed files with 90 additions and 13 deletions

View File

@@ -36,3 +36,62 @@ export const extractErrorMessage = (error: unknown): string => {
return "";
};
/**
* 将已知的 MCP 相关后端错误(通常为中文硬编码)映射为 i18n 文案
* 采用包含式匹配,尽量稳健地覆盖不同上下文的相似消息。
* 若无法识别,返回空字符串以便调用方回退到原始 detail 或默认 i18n。
*/
export const translateMcpBackendError = (
message: string,
t: (key: string, opts?: any) => string,
): string => {
if (!message) return "";
const msg = String(message).trim();
// 基础字段与结构校验相关
if (msg.includes("MCP 服务器 ID 不能为空")) {
return t("mcp.error.idRequired");
}
if (
msg.includes("MCP 服务器定义必须为 JSON 对象") ||
msg.includes("MCP 服务器 '" /* 不是对象 */) ||
msg.includes("不是对象") ||
msg.includes("服务器配置必须是对象")
) {
return t("mcp.error.jsonInvalid");
}
if (msg.includes("MCP 服务器 type 必须是")) {
return t("mcp.error.jsonInvalid");
}
// 必填字段
if (
msg.includes("stdio 类型的 MCP 服务器缺少 command 字段") ||
msg.includes("必须包含 command 字段")
) {
return t("mcp.error.commandRequired");
}
if (
msg.includes("http 类型的 MCP 服务器缺少 url 字段") ||
msg.includes("必须包含 url 字段") ||
msg === "URL 不能为空"
) {
return t("mcp.wizard.urlRequired");
}
// 文件解析/序列化
if (
msg.includes("解析 ~/.claude.json 失败") ||
msg.includes("解析 config.toml 失败") ||
msg.includes("无法识别的 TOML 格式") ||
msg.includes("TOML 内容不能为空")
) {
return t("mcp.error.tomlInvalid");
}
if (msg.includes("序列化 config.toml 失败")) {
return t("mcp.error.tomlInvalid");
}
return "";
};