Skip to content

ui: 设置→评估页 10 处错误提示显示 IPC 原始英文 message,而按 code 出本地化文案的 errors.* 机制全仓无人调用 - #297

Merged
helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-eval-settings-error-code-copy
Oct 18, 2026
Merged

helsome merged 1 commit into
helsome:mainfrom
wxrbyte:codex/fix-eval-settings-error-code-copy

Conversation

@wxrbyte

@wxrbyte wxrbyte commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

问题与修复

Closes #296

设置 → 评估页的 10 处失败提示都写成「优先用 IPC 信封里的原始 message」,而 error.message 通常都存在,?? t(...) 那一侧几乎永远不生效 —— 本地化文案被架空。界面语言 zh-CN、evaluation.getSettings 返回 { ok:false, error:{ code:'STORAGE_WRITE_FAILED', message:'STORAGE_WRITE_FAILED: connection is locked' } } 时,用户看到的是本地化标题 + 一行未翻译的英文,且把内部 code 一起暴露:

无法保存更改
STORAGE_WRITE_FAILED: connection is locked

仓库其实已经为「按稳定 code 出本地化文案」建好了完整机制:packages/i18n/src/locales/{en-US,zh-CN}/errors.ts(34 条用户可见文案)+ packages/i18n/src/error-messages.ts 的 ERROR_CODE_TO_KEY(33 条 code → key 静态表)/ errorKeyForCode / translateErrorCode。但全仓检索后,这三个符号的引用只有三处、全部在机制自己内部(定义、它自己的单测 packages/i18n/src/i18n.test.ts、barrel packages/i18n/src/index.ts),没有任何生产代码调用 —— 这条设计路径从表到解析器齐备,却没接上任何一个渲染面。

修复:加一个 4 行 helper,把解析顺序定成「注册过的 code → 本地化文案;未注册的 code → 保留 sanitized message;两者都没有 → 页面原有的 settings.evaluation.* 兜底键」,10 处调用点改为调它。

function errorCopy(t: TFunction, error: ApiError | undefined, fallbackKey: string): string {
  return translateErrorCode(error?.code, t) ?? error?.message ?? t(fallbackKey);
}
位置(3a17eca6) 改动
EvaluationSettingsTab.tsx:1-6 新增 import type { TFunction } from 'i18next';、translateErrorCode 自 @finagent/i18n,并把 ApiError 加入原有的 @finagent/core 类型 import
:36-38 新增上述 errorCopy helper
:90 :118 :137 :161 :181 :194 :207 :228 :251 :265 result?.error.message ?? t('<原键>') → errorCopy(t, result?.error, '<原键>')

不删除未注册 code 的原始 message(那是有用的诊断信息),只是不再让它抢占已注册 code 的本地化文案。errorCopy 需要真实 TFunction(可选项无法通过收窄签名表达),因此这里的类型用 i18next 的公开类型而非本地函数形状。

零新增 i18n 键:errors.* 与 settings.evaluation.* 都已存在,bun run i18n:check 的 en-US 1487 ≡ zh-CN 1487 不变。

可复现测试报告

环境:Windows 10.0.26200.9457(x64)、Bun 1.4.2、main 3a17eca6。

在既有 packages/ui/src/components/evaluation/evaluation.test.tsx 追加 2 个用例(用 client.prefs.get 把界面语言钉成 zh-CN,再把 evaluation.getSettings 桩成失败):

  • 「注册过的 code 走 errors.* 本地化文案」;
  • 「未注册的 code 仍保留原始 message」(防止修复过度)。

修复前:该文件 6 pass / 1 fail,唯一失败即上文的 Expected "无法保存更改" / Received "STORAGE_WRITE_FAILED: connection is locked"。另一个新用例本来就通过 —— 这正是「保留原始 message」这一半契约已经正确、缺的只是「已注册 code 没被解析」的直接证据。

修复后:该文件 7 pass / 0 fail。

工作区回归:

命令 结果
bun test packages/ui --isolate 344 pass / 0 fail(63 个文件)
bun x tsc --noEmit exit 0
bun run i18n:check key parity: en-US 1487 keys ≡ zh-CN 1487 keys (0 issues)

上游 PR checks(.github/workflows/pr.yml)当前 state=disabled_manually,新建 PR 不会产生 check run,故以上均为本地等价命令结果,非 CI 结论。

范围

…e map

The settings -> evaluation page surfaced IPC failures by printing the raw
`error.message` a channel returned. Those messages are English-only strings
produced in the main process, so a zh-CN user got a localized dialog title
followed by an untranslated detail line, e.g.

    无法保存更改
    STORAGE_WRITE_FAILED: connection is locked

The repo already ships the intended mechanism for this: every failure that
crosses the IPC boundary carries a SCREAMING_SNAKE `code`, and
`packages/i18n/src/error-messages.ts` maps roughly a third of those codes
onto the `errors.*` namespace, which is translated in both locales. The
resolver (`translateErrorCode`) and its table existed but had no production
caller anywhere in the tree, so the localized copy was unreachable.

This wires the page up to it:

- add a small `errorCopy(t, error, fallbackKey)` helper that resolves the
  code first, falls back to the raw message, and only then to the existing
  per-action key, so unregistered codes keep their previous text exactly;
- replace the 10 `result?.error.message ?? t(...)` sites with it.

The helper needs the real `TFunction` (options are not expressible through
the previous narrowed signature), so the import is widened to the i18next
type rather than a local function shape.

Both new cases fail before this change and pass after:
- a registered code renders `errors.*` copy instead of the raw message,
- an unregistered code still renders its raw message (the fallback contract).

Verification (local; upstream CI is disabled_manually, so no check runs are
produced for this PR):
- `bun test packages/ui --isolate` -> 344 pass / 0 fail (63 files)
- `bunx tsc --noEmit` -> exit 0
- `bun run i18n:check` -> key parity en-US 1487 = zh-CN 1487, 0 issues

Closes helsome#296

@helsome helsome left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独立代码审查及本轮精确 HEAD 验证通过,focused 7 pass / 0 fail。按本批顺序累计树完整测试 1856 pass / 7 skip / 0 fail,全 workspace typecheck、i18n 通过,新增变更 secret 扫描无命中。仅接受本 PR 声明范围,不将离线回归/SSR 视为完整桌面或真实模型验收。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ui: 设置→评估页 10 处错误提示显示 IPC 原始英文 message,而按 code 出本地化文案的 errors.* 机制全仓无人调用

2 participants