diff --git a/web/src/reviewRelease.test.ts b/web/src/reviewRelease.test.ts index f739585..393cac3 100644 --- a/web/src/reviewRelease.test.ts +++ b/web/src/reviewRelease.test.ts @@ -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') + }) +}) diff --git a/web/src/reviewRelease.ts b/web/src/reviewRelease.ts index b63a7d4..23d34ef 100644 --- a/web/src/reviewRelease.ts +++ b/web/src/reviewRelease.ts @@ -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 = { + '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 { + 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 { 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 { 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() }