fix(ui): 版本记录错误详情复用已解析响应体,不再二次读 Body
build / build (push) Successful in 1m21s

父复核发现的确定缺陷:fetchVersionHistory 在 130 行已 await response.json(),
错误分支又读一次,真实 Response 抛 TypeError(Body already read),被 catch 吞掉,
用户仍只看到状态码。

改为复用已解析的 value(error/code);同时把 code 从错误分支里重复声明去掉。
测试用真实 Response 构造 500:伪 mock 可重复 json() 会把 double-read 掩盖成绿。
This commit is contained in:
姚章浩
2026-11-01 19:59:28 +08:00
parent 8ce78e28fa
commit 30686dd288
3 changed files with 25 additions and 11 deletions
+4
View File
@@ -11,8 +11,12 @@
- 版本记录页:请求失败时不再声称"当前运行版本未知"或"版本记录未接线",并显示服务端返回的
`error`/`code`(例如 `version_history_unavailable`),把"未配置"和"后端故障"分开。
- 版本记录客户端:500/409 的异常消息复用已解析的响应体,不再二次 `response.json()`
(真实 `Response` 上会抛 `Body already read`,被 catch 吞掉后只剩状态码);错误详情现在稳定带出服务端 `error` 与 `code`。
### Tests
- `web/src/versionHistory.test.ts`:用真实 `Response` 构造 500,断言消息含 `error` 与 `code`(伪 mock 可重复 `json()` 会掩盖 double-read)。
- `internal/platform/postgres/version_history_sync_state_integration_test.go`:进行中 run(NULL finished_at)
读取同步状态与分页都必须成功。
- `web/src/releaseTargetsMaintenance.test.tsx`:已释放的失败作业不出现 held 文案;仍持有时保留。
+17
View File
@@ -6,6 +6,11 @@ const jsonResponse = (body: unknown, status = 200) =>
afterEach(() => vi.unstubAllGlobals())
// 用真实 Response(Node 全局):正文只能读一次,重复 json() 必然抛 TypeError。
// 依赖"可重复 json()"的伪 mock 会把 double-read 的 bug 掩盖成绿。
const realResponse = (body: unknown, status: number) =>
new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
const pageBody = {
items: [
{
@@ -48,6 +53,18 @@ const pageBody = {
},
}
describe('fetchVersionHistory 服务端错误详情', () => {
it('500 时把服务端的 error 与 code 带进异常消息(真实 Response,正文只能读一次)', async () => {
const errBody = { error: '读取版本记录失败', code: 'version_history_unavailable' }
const fetchMock = vi.fn(() => Promise.resolve(realResponse(errBody, 500)))
vi.stubGlobal('fetch', fetchMock)
await expect(fetchVersionHistory({ kind: 'all', cursor: '' })).rejects.toThrow(
/HTTP 500.*读取版本记录失败.*version_history_unavailable/s
)
expect(fetchMock).toHaveBeenCalledOnce()
})
})
describe('versionHistory client', () => {
it('loads a page, keeps the cursor and reads sync/current-running facts', async () => {
const fetchMock = vi.fn((_input: RequestInfo | URL, _init?: RequestInit) => jsonResponse(pageBody))
+4 -11
View File
@@ -133,17 +133,10 @@ export async function fetchVersionHistory(
if (response.status === 409 || code === 'cursor_expired') {
throw new Error('这次翻页依据的记录快照已被清理,请刷新页面重新加载。')
}
// 把服务端的 code/message 带出来:否则页面只能报状态码,用户无法区分
// "未配置"、"同步进行中导致 500" 和真正的后端故障。
let detail = ''
try {
const body = await response.json()
const message = record(body) ? text(body.error) : ''
const code = record(body) ? text(body.code) : ''
if (message || code) detail = `:${message || '无错误描述'}${code ? `(${code})` : ''}`
} catch {
detail = ''
}
// 正文在上面已经解析进 value 了;再调一次 response.json() 会抛 TypeError
// (Body already read),被吞掉后用户只能看到状态码。直接复用 value。
const message = record(value) ? text(value.error) : ''
const detail = message || code ? `:${message || '无错误描述'}${code ? `(${code})` : ''}` : ''
throw new Error(`加载版本记录失败(HTTP ${response.status})${detail}`)
}
if (!record(value) || !Array.isArray(value.items) || !record(value.sync) || !record(value.current_running)) {