fix(前端): 浏览器布局告警不再当成页面出错(后管红条 / 用户端中文提示的误报)
客户实况(2026-11-27,外观专利数据库页):右下角红条 「页面出错了:ResizeObserver loop completed with undelivered notifications.(已记录,刷新可恢复…)」。 这条是 Chrome/Edge 在 ResizeObserver 回调里同一帧改尺寸没送完时给的**警告**:没有异常对象、 没有堆栈、页面功能完好(余下的通知延到下一帧)。页面重排/窗口缩放都会命中,于是: 1) 误报「页面出错了」,客户以为功能坏了; 2) 每条都走 sendBeacon 打 /api/admin/client-errors,把真错误的现场冲成噪声。 修法=在两个前端各自的**唯一错误出口**加一道良性闸(同一把尺,两处对称): - 后管 `shared/api/benign-browser-warnings.ts`(新)收 Chrome 现行文案与旧版/Safari 的 `ResizeObserver loop limit exceeded`,按**前缀全等**匹配(截断文案、大小写变体、前置了 别的内容一律不认,防止把真异常洗白);`error-report.ts::reportClientError` 命中即只落一行 console.debug,不弹红条、不进内存环、不上报。 - 用户端 `shared/utils/app-error.ts` 同一清单与判据,`report()` 命中即 console.debug 后返回, 不弹「页面出了点问题」、不挤进 20 条记录环(挤掉真错误比不记录更糟)。 判据:后管 `tests/benign-browser-warnings.test.ts` 4 支(两条浏览器文案判良性/真异常与截断文案 不许判良性/清单只能是 ResizeObserver 那两条/出口真接线:良性不进环、真异常照旧进环); 用户端 `tests/global-error-handler.test.ts` 新增 1 支(message 与 Error 两种形态都不弹不记, 真异常在同一世界照旧三出口全响)。两向反向验证:把闸改成 `false &&` 后后管 1 红、用户端 1 红, 复原即绿。 门禁:后管 `node --test` 1975 绿+`vitest` 253 绿+`vue-tsc` rc=0; 用户端 `node --test` 1150 绿+`vitest` 149 绿+`vue-tsc` rc=0。
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* 浏览器布局告警不许当成页面出错(2026-11-27 客户实况:外观专利数据库页弹
|
||||
* `页面出错了:ResizeObserver loop completed with undelivered notifications.`)。
|
||||
*
|
||||
* 两半判据:
|
||||
* ① 纯函数:两条浏览器文案判良性,别的(真异常、截断文案、空串)都不许判良性——
|
||||
* 清单放宽等于把真异常洗白,收窄等于把误报还给用户;
|
||||
* ② 出口行为:`reportClientError` 对良性告警**不进内存环**(不弹红条、不上报都挂在同一个早返回上),
|
||||
* 对真异常照旧进环——只钉纯函数会漏掉「判了但没接线」。
|
||||
*/
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
BENIGN_BROWSER_WARNING_PREFIXES,
|
||||
isBenignBrowserWarning,
|
||||
} from '../src/shared/api/benign-browser-warnings.ts'
|
||||
import { recentClientErrors, reportClientError } from '../src/shared/api/error-report.ts'
|
||||
|
||||
test('两条浏览器布局告警判良性:Chrome 现行文案(带句点)与旧版/Safari 文案', () => {
|
||||
assert.equal(isBenignBrowserWarning('ResizeObserver loop completed with undelivered notifications.'), true)
|
||||
assert.equal(isBenignBrowserWarning('ResizeObserver loop limit exceeded'), true)
|
||||
// 浏览器有时会在同一句后面再拼一段,前缀匹配要照样认
|
||||
assert.equal(
|
||||
isBenignBrowserWarning('ResizeObserver loop completed with undelivered notifications. at flush()'),
|
||||
true,
|
||||
)
|
||||
// 前后空白不影响判定(ErrorEvent.message 可能带)
|
||||
assert.equal(isBenignBrowserWarning(' ResizeObserver loop limit exceeded '), true)
|
||||
})
|
||||
|
||||
test('真异常不许被这张清单洗白', () => {
|
||||
const notBenign = [
|
||||
'',
|
||||
' ',
|
||||
'Uncaught TypeError: Cannot read properties of undefined',
|
||||
'Network Error',
|
||||
'ResizeObserver loop completed', // 截断文案:不是浏览器原话,不能认
|
||||
'resizeobserver loop limit exceeded', // 大小写不同:浏览器不会这么发
|
||||
'页面出了点问题,这一步没能完成。',
|
||||
'Uncaught ResizeObserver loop limit exceeded', // 前置了别的内容,不是纯告警
|
||||
]
|
||||
for (const text of notBenign) {
|
||||
assert.equal(isBenignBrowserWarning(text), false, `不该判良性:${JSON.stringify(text)}`)
|
||||
}
|
||||
})
|
||||
|
||||
test('清单只许是 ResizeObserver 那两条(防止有人往里加通用错误文案)', () => {
|
||||
assert.equal(BENIGN_BROWSER_WARNING_PREFIXES.length, 2)
|
||||
for (const prefix of BENIGN_BROWSER_WARNING_PREFIXES) {
|
||||
assert.match(prefix, /^ResizeObserver loop /, `良性清单里混进了非布局告警:${prefix}`)
|
||||
}
|
||||
})
|
||||
|
||||
test('出口真接线:良性告警不进内存环,真异常照旧进环', () => {
|
||||
const before = recentClientErrors().length
|
||||
const debugged: string[] = []
|
||||
const originalDebug = console.debug
|
||||
console.debug = (...args: unknown[]) => { debugged.push(args.map(String).join(' ')) }
|
||||
try {
|
||||
reportClientError({
|
||||
kind: 'window-error',
|
||||
message: 'ResizeObserver loop completed with undelivered notifications.',
|
||||
route: '/patent-center/clean-items',
|
||||
})
|
||||
} finally {
|
||||
console.debug = originalDebug
|
||||
}
|
||||
assert.equal(recentClientErrors().length, before, '良性告警进了内存环 ⇒ 红条/上报那条路也会走')
|
||||
assert.equal(debugged.length, 1, '良性告警要留一行 console.debug,不能连留痕都没有')
|
||||
assert.match(debugged[0], /benign/)
|
||||
|
||||
reportClientError({
|
||||
kind: 'window-error',
|
||||
message: 'Uncaught TypeError: 良性告警测试用的真异常',
|
||||
route: '/patent-center/clean-items',
|
||||
})
|
||||
assert.equal(recentClientErrors().length, before + 1, '真异常必须照旧进环(判据自己不能把错误吞掉)')
|
||||
})
|
||||
Reference in New Issue
Block a user