fix(web): 大纲错误透传 FriendlyError, 不再向用户暴露内部 code

This commit is contained in:
Yaojia Wang
2026-06-29 16:52:43 +02:00
parent 4afbbe0fe7
commit ffe9132f7d
3 changed files with 40 additions and 20 deletions

View File

@@ -61,7 +61,7 @@ describe("useOutline", () => {
expect(result.current.chapters).toEqual([]);
});
it("后端返回业务错误:status=error 并 surface 友好文案", async () => {
it("后端返回业务错误:透传 friendly 文案 + 跳转动作(不再固定 OUTLINE_FAILED", async () => {
post.mockResolvedValue({
data: null,
error: { error: { code: "LLM_UNAVAILABLE" } },
@@ -72,18 +72,36 @@ describe("useOutline", () => {
});
expect(result.current.status).toBe("error");
expect(result.current.error?.code).toBe("OUTLINE_FAILED");
expect(result.current.error?.message).toContain("provider");
// LLM_UNAVAILABLE → 透传 provider 文案 + 「去设置提供商」链接。
expect(result.current.error?.text).toContain("provider");
expect(result.current.error?.actionHref).toBe("/settings/providers");
expect(result.current.error?.actionLabel).toBe("去设置提供商");
expect(toast).toHaveBeenCalledWith(expect.any(String), "error");
});
it("data 为空且无 error 也走错误分支", async () => {
it("校验错误422 detail 信封):透传 VALIDATION 文案且无跳转动作", async () => {
post.mockResolvedValue({
data: null,
error: { detail: [{ msg: "字段缺失" }] },
});
const { result } = renderHook(() => useOutline([]));
await act(async () => {
await result.current.generate("p1", 1);
});
expect(result.current.status).toBe("error");
expect(result.current.error?.text).toBeTruthy();
expect(result.current.error?.actionHref).toBeUndefined();
expect(result.current.error?.actionLabel).toBeUndefined();
});
it("data 为空且无 error 也走错误分支(兜底文案、无跳转)", async () => {
post.mockResolvedValue({ data: null, error: null });
const { result } = renderHook(() => useOutline([]));
await act(async () => {
await result.current.generate("p1", 1);
});
expect(result.current.status).toBe("error");
expect(result.current.error?.code).toBe("OUTLINE_FAILED");
expect(result.current.error?.text).toBeTruthy();
expect(result.current.error?.actionHref).toBeUndefined();
});
});