diff --git a/CHANGELOG.md b/CHANGELOG.md index 3719f76..221bb1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Failures reading a response body are now thrown as `GradientLabsError` with + the original `cause`, instead of propagating as a raw error. +- The README webhook example now handles `UnknownWebhookTypeError` by + responding 200, matching `examples/webhooks`. + ## [0.1.3] ### Removed diff --git a/README.md b/README.md index 7fb48af..4b957d8 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,11 @@ incoming requests. Pass the **raw** request body — the signature is computed o the exact bytes received. ```ts -import { GradientLabs, InvalidWebhookSignatureError } from "@gradientlabs/client"; +import { + GradientLabs, + InvalidWebhookSignatureError, + UnknownWebhookTypeError, +} from "@gradientlabs/client"; const client = new GradientLabs({ apiKey: process.env.GRADIENT_LABS_API_KEY!, @@ -184,6 +188,10 @@ app.post("/webhooks", express.raw({ type: "*/*" }), (req, res) => { } catch (err) { if (err instanceof InvalidWebhookSignatureError) { res.sendStatus(401); + } else if (err instanceof UnknownWebhookTypeError) { + // Log and acknowledge so Gradient Labs does not retry. + console.warn("Unknown webhook type:", err.type); + res.sendStatus(200); } else { res.sendStatus(500); } diff --git a/src/internal/http.ts b/src/internal/http.ts index d56a4b4..4c9a873 100644 --- a/src/internal/http.ts +++ b/src/internal/http.ts @@ -65,7 +65,14 @@ export class HttpClient { }); } - const rawBody = await response.text(); + let rawBody: string; + try { + rawBody = await response.text(); + } catch (cause) { + throw new GradientLabsError(`failed to read response body: ${errorMessage(cause)}`, { + cause, + }); + } if (response.status < 200 || response.status > 299) { throw toApiError(response.status, rawBody); diff --git a/test/http.test.ts b/test/http.test.ts index c330c20..4fcd7dc 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; -import { ApiError, GradientLabs, type FetchLike } from "../src/index.js"; +import { ApiError, GradientLabs, GradientLabsError, type FetchLike } from "../src/index.js"; interface RecordedRequest { input: string; @@ -302,3 +302,17 @@ describe("HttpClient", () => { ).rejects.toThrow(); }); }); + +describe("response body read failures", () => { + it("wraps them in GradientLabsError with the cause preserved", async () => { + const original = new Error("synthetic body failure"); + const client = new GradientLabs({ + apiKey: "key", + fetch: () => Promise.resolve({ status: 200, text: () => Promise.reject(original) }), + }); + + const err = await client.conversations.get("conv_1").catch((e: unknown) => e); + expect(err).toBeInstanceOf(GradientLabsError); + expect((err as GradientLabsError).cause).toBe(original); + }); +});