fix(qa): 前端错误文案——按 API 信封 surface 真实错因,去掉通用 toast
新增 friendlyFromApiError(apiError):兼容业务信封 {error:{code,message}}(ARCH §7.1)
与 FastAPI 422 {detail:[...]},422 映射为友好 VALIDATION 文案(不泄露技术细节)。
接入三处此前吞掉错因、永显通用文案的调用点:
- useOutline(QA #2):原 env.error.code 漏读 422 detail,永显「大纲生成失败」。
- ChainPage / ChainAdjudication(QA #11):原 friendlyError(undefined) 永显通用 toast,
现 surface 真实 code(如 LLM_UNAVAILABLE→去设置 provider / CONFLICT→提示裁决)。
+ messages.test.ts 加 3 例(业务码带动作 / 422→VALIDATION / 未知形兜底)。
门禁绿:vitest / tsc / eslint 干净。
This commit is contained in:
@@ -4,6 +4,7 @@ import { useCallback, useState } from "react";
|
||||
|
||||
import { api } from "@/lib/api/client";
|
||||
import { useToast } from "@/components/Toast";
|
||||
import { friendlyFromApiError } from "@/lib/errors/messages";
|
||||
import type { OutlineChapterView } from "@/lib/api/types";
|
||||
|
||||
export type OutlineStatus = "idle" | "generating" | "ready" | "error";
|
||||
@@ -42,14 +43,12 @@ export function useOutline(initial: OutlineChapterView[]): UseOutline {
|
||||
},
|
||||
);
|
||||
if (apiError || !data) {
|
||||
const env = apiError as
|
||||
| { error?: { code?: string; message?: string } }
|
||||
| undefined;
|
||||
const code = env?.error?.code ?? "OUTLINE_FAILED";
|
||||
const message = env?.error?.message ?? "大纲生成失败,请重试。";
|
||||
setError({ code, message });
|
||||
// surface 真实错因(如 LLM_UNAVAILABLE→去设置 provider;422→校验文案),
|
||||
// 而非永远的通用「大纲生成失败」(QA #2:原 env.error.code 漏读 422 的 detail 信封)。
|
||||
const friendly = friendlyFromApiError(apiError);
|
||||
setError({ code: "OUTLINE_FAILED", message: friendly.text });
|
||||
setStatus("error");
|
||||
toast(message, "error");
|
||||
toast(friendly.text, "error");
|
||||
return;
|
||||
}
|
||||
setChapters(data.chapters ?? []);
|
||||
|
||||
Reference in New Issue
Block a user