Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion src/internal/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 15 additions & 1 deletion test/http.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});
});
Loading