From 64692fecba3519d36f7f9d2e78af7d1b5649f325 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Thu, 10 Sep 2026 23:58:28 +0800 Subject: [PATCH] Fix HTTP error response body reporting --- src/core/http/factory.test.ts | 33 +++++++++++++++++++++++ src/core/http/factory.ts | 51 +++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 src/core/http/factory.test.ts diff --git a/src/core/http/factory.test.ts b/src/core/http/factory.test.ts new file mode 100644 index 0000000..6d0ff41 --- /dev/null +++ b/src/core/http/factory.test.ts @@ -0,0 +1,33 @@ +import { afterEach, describe, expect, test } from "bun:test"; +import { createSealClientForUrl } from "./factory.js"; + +const servers: Array> = []; + +afterEach(() => { + for (const server of servers.splice(0)) server.stop(true); +}); + +describe("Seal HTTP client errors", () => { + test("includes ky's parsed response body after the response stream is consumed", async () => { + const server = Bun.serve({ + port: 0, + fetch: () => new Response(JSON.stringify({ + error: { + code: "error:common:invalid-parameter", + message: "contentType 取值非法" + } + }), { + status: 400, + headers: { "content-type": "application/json" } + }) + }); + servers.push(server); + + await expect( + createSealClientForUrl(`http://127.0.0.1:${server.port}`, "dummy-token").get("/") + ).rejects.toThrow( + 'Request failed with status code 400 Bad Request: GET http://127.0.0.1:' + + `${server.port}/: {"error":{"code":"error:common:invalid-parameter","message":"contentType 取值非法"}}` + ); + }); +}); diff --git a/src/core/http/factory.ts b/src/core/http/factory.ts index a0d2889..10a9aa8 100644 --- a/src/core/http/factory.ts +++ b/src/core/http/factory.ts @@ -3,6 +3,32 @@ import { CorpConfig } from "../config/types.js"; import { clearSealToken, getSealSessionForCorp } from "../auth/seal.js"; import { resolveSealCustomHeaders } from "./request-headers.js"; +function formatErrorData(data: unknown): string { + if (data === undefined || data === null || data === "") return ""; + if (typeof data === "string") return data; + try { + return JSON.stringify(data); + } catch { + return String(data); + } +} + +function withResponseBody(error: unknown): Error { + if (!(error instanceof HTTPError)) return error as Error; + const body = formatErrorData(error.data); + if (body) error.message = `${error.message}: ${body}`; + return error; +} + +function createBeforeErrorHook(corpId?: string) { + return async ({ error }: { error: unknown }) => { + if (corpId && error instanceof HTTPError && error.response.status === 401) { + clearSealToken(corpId); + } + return withResponseBody(error); + }; +} + export async function createSealClient(corp: CorpConfig): Promise { const session = await getSealSessionForCorp(corp); @@ -14,19 +40,7 @@ export async function createSealClient(corp: CorpConfig): Promise { Authorization: `Bearer ${session.token}` }, hooks: { - beforeError: [ - async ({ error }) => { - if (error instanceof HTTPError && error.response.status === 401) { - clearSealToken(corp.id); - } - const body = - error instanceof HTTPError - ? await error.response.text().catch(() => "") - : ""; - error.message = `${error.message}${body ? `: ${body}` : ""}`; - return error; - } - ] + beforeError: [createBeforeErrorHook(corp.id)] } }); } @@ -50,16 +64,7 @@ export function createSealClientForUrl( Authorization: `Bearer ${token}` }, hooks: { - beforeError: [ - async ({ error }) => { - const body = - error instanceof HTTPError - ? await error.response.text().catch(() => "") - : ""; - error.message = `${error.message}${body ? `: ${body}` : ""}`; - return error; - } - ] + beforeError: [createBeforeErrorHook()] } }); }