fix(web): 建批失败只显示 HTTP 状态码 —— 把服务端原因与下一步带出来
写操作的服务端一直回 {"error": "<原因>"},但 getJSON/sendJSON 只把状态码拼进错误消息,
运维看到的是「创建封批失败(HTTP 422)」,无从下手。实测真实原因只有两种,且都有明确
下一步:所选知识不在已冻结的发布清单里(先「生成新版本」+「确认冻结」)、或所选知识当前
不满足发布条件。现在把原因带出,并对这两种已知原因直接给出下一步;其他原因原样展示。
背景(本次 422 的根因):ATOM群体广播控制方案使用方法.adoc 的批准时间 06:07:05Z 晚于
已冻结版本 v8 的冻结时间 03:23:48Z,v8 的 113 行里没有它的行,绑定校验因此不通过。
发布计划页显示的是实时计划(所以写着"可加入发布批次"),建批绑定的是已冻结版本——两者
不一致时必须先重新生成版本,这条顺带值得在产品上更醒目地提示。
测试:reviewRelease.test.ts 补 2 条(覆盖清单不足时给出下一步 / 未知原因原样带出)。
This commit is contained in:
@@ -225,3 +225,21 @@ describe('production actions', () => {
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
})
|
||||
|
||||
describe('建批失败时把服务端原因带出来', () => {
|
||||
it('覆盖清单不足时给出下一步(生成新版本 + 确认冻结)', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: false,
|
||||
status: 422,
|
||||
json: async () => ({ error: 'release change set must be confirmed and cover every requested publication' }),
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
await expect(createReleaseBatch('验收12', ['tp-x'], [], 'rcs-x')).rejects.toThrow('请先点「生成新版本」再「确认冻结(此后不可更改)」')
|
||||
})
|
||||
|
||||
it('未知原因原样带出,不再只给状态码', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 500, json: async () => ({ error: 'could not create release batch' }) })
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
await expect(createReleaseBatch('验收12', ['tp-x'], [], '')).rejects.toThrow('could not create release batch')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -478,15 +478,33 @@ function parsePageEnvelope(value: unknown, label: string) {
|
||||
return { rawItems: value.data, total: value.total, limit: value.limit, offset: value.offset }
|
||||
}
|
||||
|
||||
// 服务端对写操作会回 {"error": "<原因>"}。旧写法只显示 HTTP 状态码,等于把原因丢掉——
|
||||
// 运维看到「创建封批失败(HTTP 422)」完全无从下手(实测:真实原因是"所选知识不在已冻结的
|
||||
// 发布清单里",先点「生成新版本」+「确认冻结」即可)。这里把原因带上,并对已知原因给出下一步。
|
||||
const releaseFailureHints: Record<string, string> = {
|
||||
'release change set must be confirmed and cover every requested publication':
|
||||
':所选知识不在已冻结的发布清单里 —— 请先点「生成新版本」再「确认冻结(此后不可更改)」,然后重新建批',
|
||||
'release batch contains ineligible publications':
|
||||
':所选知识当前不满足发布条件(需重新确认审核结论/测试结果/发布范围)',
|
||||
'current release plan has no releasable item': ':当前发布计划里没有可发布项',
|
||||
}
|
||||
|
||||
async function failureMessage(response: Response, message: string): Promise<string> {
|
||||
const body = await response.json().catch(() => null) as unknown
|
||||
const reason = isRecord(body) && typeof body.error === 'string' ? body.error : ''
|
||||
if (!reason) return `${message}(HTTP ${response.status})`
|
||||
return `${message}(HTTP ${response.status})${releaseFailureHints[reason] ?? `:${reason}`}`
|
||||
}
|
||||
|
||||
async function getJSON(url: string, signal: AbortSignal | undefined, message: string): Promise<unknown> {
|
||||
const response = await fetch(url, { headers: { Accept: 'application/json' }, signal })
|
||||
if (!response.ok) throw new Error(`${message}(HTTP ${response.status})`)
|
||||
if (!response.ok) throw new Error(await failureMessage(response, message))
|
||||
return response.json()
|
||||
}
|
||||
|
||||
async function sendJSON(url: string, body: unknown, message: string): Promise<unknown> {
|
||||
const response = await fetch(url, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'Idempotency-Key': newOperationID() }, body: JSON.stringify(body) })
|
||||
if (!response.ok) throw new Error(`${message}(HTTP ${response.status})`)
|
||||
if (!response.ok) throw new Error(await failureMessage(response, message))
|
||||
return response.json()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user