282ad7be9f
后端仍有出口把底层异常原文写进 error_message / fileError / 匹配原因 / 响应 message,逐条翻译
跟不上新增;用户 2026-10-09 要求整个项目的提示语都用中文,所以把规矩做进界面出口。
规则:原文含中文照原样透出;整条无中文换成中文兜底句;**含中文但冒号后是纯英文尾巴的剥掉尾巴**
("转换结果打包失败: No space left on device" 只留主干)。被隐去的原文进 console 供核对日志。
闸口:用户端 http.ts 的 extractErrorMessage 五条返回路径与 unwrapJavaResponse、后台 envelope 的
textOf/errorTextOf、13 处任务卡 `错误:${item.error}`、2 处 fileError 兜底句、发布页文件错误与
匹配原因函数、dispatch-guard 的解析失败原因清单、22 处 ElMessage.error(result.error) 型展示、
后台看板与数字人详情页两处模板。
尺子:frontend-vue/tests/user-notice-chinese-only-guard.test.ts、admin-frontend-vue/tests/user-notice-chinese-only.test.ts
(分母硬钉+内存造违例自证+摘壳写法仍计入分母);source-pin 台账只登记本轮新增那一个文件,
别人的在飞格子没重算。node --test 用户端 782/782、后台 1782/1782,两棵树 vue-tsc 均 rc=0。
140 lines
7.0 KiB
TypeScript
140 lines
7.0 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { readFileSync, readdirSync } from 'node:fs'
|
||
import { fileURLToPath } from 'node:url'
|
||
import { resolve, dirname, join } from 'node:path'
|
||
import { userNotice } from '../src/shared/utils/user-notice.ts'
|
||
|
||
/**
|
||
* 全项目中文文案尺子(用户端):界面拿到的提示语必须是中文。
|
||
*
|
||
* 起因是 2026-10-09 那张货源查询截图,卡片上出现
|
||
* `Similar ASIN result rows are empty; please retry result generation`——那不是写错的文案,
|
||
* 而是后端把底层异常原文存进了 error_message,前端不加转换地渲染。后端逐条翻译追不上新增,
|
||
* 所以这条规矩做进界面出口:`userNotice()` 只放行含中文的串,英文一律换成中文兜底句,原文进 console。
|
||
*
|
||
* 三把尺子:① 行为钉子;② 任务卡 `错误:${...}` 与 payload 展示必须走 userNotice;
|
||
* ③ http.ts 的每条返回路径都要过闸。分母硬钉,防止扫描器失明后"零违规"是假的。
|
||
*/
|
||
|
||
const here = dirname(fileURLToPath(import.meta.url))
|
||
const SRC = resolve(here, '../src')
|
||
|
||
function sourceFiles(dir: string): string[] {
|
||
const out: string[] = []
|
||
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
||
if (entry.name === 'node_modules' || entry.name === 'dist') continue
|
||
const full = join(dir, entry.name)
|
||
if (entry.isDirectory()) {
|
||
out.push(...sourceFiles(full))
|
||
} else if (entry.name.endsWith('.vue') || entry.name.endsWith('.ts')) {
|
||
out.push(full)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
const CALL = /(ElMessage\.(?:error|warning)\(|showStatus\()/
|
||
const ARG = /^((?:res|result|payload|data|resp|response)\??\.(?:error|message))/
|
||
|
||
function read(path: string): string {
|
||
return readFileSync(path, 'utf-8')
|
||
}
|
||
|
||
test('userNotice 放行中文、替换英文、空串交回调用方默认句', () => {
|
||
assert.equal(userNotice('这份文件没有解析到可导出的行'), '这份文件没有解析到可导出的行')
|
||
assert.equal(userNotice('Similar ASIN result rows are empty'), '操作未能完成,请稍后重试')
|
||
assert.equal(userNotice('network timeout', '任务没能跑完,请稍后重试'), '任务没能跑完,请稍后重试')
|
||
assert.equal(userNotice(' ', '没填'), '没填')
|
||
assert.equal(userNotice(null, '没填'), '没填')
|
||
// 全角标点不算中文,否则 ":()" 这类残句会被当成可读文案透出
|
||
assert.equal(userNotice(':()', '兜底'), '兜底')
|
||
})
|
||
|
||
test('中文主干后面的英文尾巴只进 console,不上界面', () => {
|
||
// 后端历史写法:new BusinessException("转换结果打包失败: " + ex.getMessage())
|
||
assert.equal(userNotice('转换结果打包失败: No space left on device'), '转换结果打包失败')
|
||
assert.equal(userNotice('生成跟价 Excel 失败:Cannot parse column'), '生成跟价 Excel 失败')
|
||
// 带中文尾巴的要留着,那是给用户看的证据
|
||
assert.equal(userNotice('仅完成 102/112 行,其余 10 行未采到数据(未检测)'),
|
||
'仅完成 102/112 行,其余 10 行未采到数据(未检测)')
|
||
assert.equal(userNotice('Coze 工作流已结束,状态 FAILED'), 'Coze 工作流已结束,状态 FAILED')
|
||
})
|
||
|
||
test('任务卡与看板上的错误行一律经 userNotice 把关', () => {
|
||
const FIELDISH = /(error|Error|message|Message)/
|
||
const INTERP = /\$\{([^{}]+)\}|\{\{\s*([^{}]+?)\s*\}\}/g
|
||
const offenders: string[] = []
|
||
let sites = 0
|
||
for (const file of sourceFiles(SRC)) {
|
||
const rel = file.slice(SRC.length + 1).replace(/\\/g, '/')
|
||
const lines = read(file).split(/\r?\n/)
|
||
for (const line of lines) {
|
||
if (!line.includes('错误')) continue
|
||
for (const m of line.matchAll(INTERP)) {
|
||
const expr = m[1] ?? m[2] ?? ''
|
||
if (!FIELDISH.test(expr)) continue
|
||
sites++
|
||
if (!expr.includes('userNotice(')) {
|
||
offenders.push(`${rel}: ${line.trim().slice(0, 120)}`)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
assert.ok(sites >= 15, `只扫到 ${sites} 处错误行插值(下限 15)——扫描器或目录树坏了,此刻的"零违规"不作数`)
|
||
assert.deepEqual(offenders, [], '这些错误行把后端原文直接拼进了界面')
|
||
})
|
||
|
||
/** 一行 payload 错误展示 → { 是否算一处出口, 是否已把关 };判据与自证共用同一个函数。 */
|
||
function payloadDisplay(line: string): { site: boolean; guarded: boolean } {
|
||
const m = line.match(CALL)
|
||
if (!m || m.index === undefined) return { site: false, guarded: true }
|
||
const raw = line.slice(m.index + m[0].length)
|
||
const guarded = raw.startsWith('userNotice(')
|
||
const rest = guarded ? raw.slice('userNotice('.length) : raw
|
||
return { site: ARG.test(rest), guarded }
|
||
}
|
||
|
||
test('payload 上的 error/message 展示一律经 userNotice 把关', () => {
|
||
const offenders: string[] = []
|
||
let sites = 0
|
||
for (const file of sourceFiles(SRC)) {
|
||
const rel = file.slice(SRC.length + 1).replace(/\\/g, '/')
|
||
for (const line of read(file).split(/\r?\n/)) {
|
||
if (!CALL.test(line)) continue
|
||
const found = payloadDisplay(line)
|
||
if (!found.site) continue
|
||
sites++
|
||
if (!found.guarded) {
|
||
offenders.push(`${rel}: ${line.trim().slice(0, 120)}`)
|
||
}
|
||
}
|
||
}
|
||
assert.ok(sites >= 20, `只扫到 ${sites} 处 payload 错误展示(下限 20)——分母缩水说明抽取器失明`)
|
||
assert.deepEqual(offenders, [], '这些提示语把响应体原文交给了界面')
|
||
})
|
||
|
||
test('http.ts 的每条取文案路径都过闸', () => {
|
||
const http = read(resolve(SRC, 'shared/api/http.ts'))
|
||
const guarded = (http.match(/userNotice\(/g) ?? []).length
|
||
assert.ok(guarded >= 5, `http.ts 里只有 ${guarded} 处 userNotice 把关(下限 5)——兜底路径漏了`)
|
||
// 反向钉:extractErrorMessage 不许再裸返回响应体字段或 axios 原文
|
||
const body = http.slice(http.indexOf('export function extractErrorMessage'), http.indexOf('/** 从 axios 错误对象里取响应体'))
|
||
assert.ok(!/\breturn data\b/.test(body), '响应体字符串不该原样透出')
|
||
assert.ok(!/return error\.message \|\|/.test(body), 'axios 英文原文不该原样透出')
|
||
})
|
||
|
||
test('判据抓得住自己造的违例', () => {
|
||
const planted = ' ...(item.error ? [`错误:${item.error}`] : []),'
|
||
assert.ok(planted.includes('错误'), '样本行该带错误标记')
|
||
assert.ok(/\$\{item\.error\}/.test(planted) && !planted.includes('userNotice('), '违例样本必须被判成未把关')
|
||
const guardedLine = ' ...(item.error ? [`错误:${userNotice(item.error)}`] : []),'
|
||
assert.ok(guardedLine.includes('userNotice('), '把关写法不能被误判成违例')
|
||
|
||
// payload 抽取器:摘壳前后都要认得出是一处出口,否则分母会静默缩水成 0
|
||
assert.deepEqual(payloadDisplay(' ElMessage.error(result.error)'), { site: true, guarded: false })
|
||
assert.deepEqual(payloadDisplay(' ElMessage.error(userNotice(result.error))'), { site: true, guarded: true })
|
||
// 常量文案不是这型出口,不计进分母
|
||
assert.deepEqual(payloadDisplay(' ElMessage.error(\'请先登录\')'), { site: false, guarded: false })
|
||
})
|