Skip to content
Merged
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
16 changes: 13 additions & 3 deletions sdks/typescript/pmxt/hosted-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,21 @@ export async function _tradingRequest(
body: opts.body !== undefined ? JSON.stringify(opts.body) : undefined,
});

const text = await resp.text();

if (!resp.ok) {
await raiseFromResponse(resp);
await raiseFromResponse(new Response(text, { status: resp.status, statusText: resp.statusText }));
}

const text = await resp.text();
if (!text) return null;
return JSON.parse(text);
const payload = JSON.parse(text);
if (
payload != null &&
typeof payload === "object" &&
((payload as Record<string, unknown>).success === false ||
typeof (payload as Record<string, unknown>).error === "string")
) {
await raiseFromResponse(new Response(text, { status: resp.status, statusText: resp.statusText }));
}
return payload;
}
34 changes: 34 additions & 0 deletions sdks/typescript/tests/hosted-routing-embedded-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { _tradingRequest } from "../pmxt/hosted-routing";
import { HostedTradingError } from "../pmxt/hosted-errors";

const client = { pmxtApiKey: "test_pmxt_key", exchangeName: "polymarket" };

afterEach(() => {
jest.restoreAllMocks();
});

function installFetchResponse(payload: unknown, status = 200): jest.SpyInstance {
return jest.spyOn(global, "fetch").mockResolvedValue(
new Response(JSON.stringify(payload), {
status,
headers: { "Content-Type": "application/json" },
}),
);
}

describe("_tradingRequest embedded error envelopes", () => {
it("raises a hosted error for 2xx responses with success=false", async () => {
installFetchResponse({ success: false, error: "upstream rejected order" }, 200);

try {
await _tradingRequest(client, { method: "GET", path: "/v0/user/me/balances" });
} catch (err) {
expect(err).toBeInstanceOf(HostedTradingError);
expect((err as HostedTradingError).status).toBe(200);
expect((err as HostedTradingError).detail).toBe("upstream rejected order");
return;
}

throw new Error("expected _tradingRequest to raise HostedTradingError");
});
});
Loading