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
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { resolveOpenClawStateDir } from "./src/utils/openclaw-state-dir.js";
export {
GatewayMemoryClient,
GatewayMemoryClientError,
GatewayMemoryClientParseError,
createGatewayPlatformAdapter,
} from "./src/adapters/gateway-client/index.js";
export type {
Expand Down
15 changes: 15 additions & 0 deletions src/adapters/gateway-client/gateway-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
GatewayMemoryClient,
GatewayMemoryClientError,
GatewayMemoryClientParseError,
createGatewayPlatformAdapter,
} from "./index.js";

Expand Down Expand Up @@ -58,6 +59,20 @@ describe("GatewayMemoryClient", () => {
responseBody: "bad query",
} satisfies Partial<GatewayMemoryClientError>);
});

it("surfaces malformed success responses with request context", async () => {
const client = new GatewayMemoryClient({
baseUrl: "http://127.0.0.1:8420",
fetchImpl: async () => new Response("not-json", { status: 200 }),
});

await expect(client.health()).rejects.toMatchObject({
name: "GatewayMemoryClientParseError",
status: 200,
path: "/health",
responseBody: "not-json",
} satisfies Partial<GatewayMemoryClientParseError>);
});
});

describe("createGatewayPlatformAdapter", () => {
Expand Down
20 changes: 19 additions & 1 deletion src/adapters/gateway-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ export class GatewayMemoryClientError extends Error {
}
}

export class GatewayMemoryClientParseError extends Error {
readonly status: number;
readonly path: string;
readonly responseBody: string;

constructor(path: string, status: number, responseBody: string) {
super(`Gateway response was not valid JSON: ${path} returned ${status}`);
this.name = "GatewayMemoryClientParseError";
this.path = path;
this.status = status;
this.responseBody = responseBody;
}
}

export class GatewayMemoryClient {
private readonly baseUrl: string;
private readonly apiKey: string | undefined;
Expand Down Expand Up @@ -136,7 +150,11 @@ export class GatewayMemoryClient {
if (!response.ok) {
throw new GatewayMemoryClientError(path, response.status, text);
}
return (text ? JSON.parse(text) : {}) as T;
try {
return (text ? JSON.parse(text) : {}) as T;
} catch {
throw new GatewayMemoryClientParseError(path, response.status, text);
}
} finally {
clearTimeout(timer);
}
Expand Down
1 change: 1 addition & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type { StandaloneHostAdapterOptions, StandaloneLLMConfig, StandaloneLLMRu
export {
GatewayMemoryClient,
GatewayMemoryClientError,
GatewayMemoryClientParseError,
createGatewayPlatformAdapter,
} from "./gateway-client/index.js";
export type {
Expand Down