Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): report a stalled error body as a timeout

Yudhi Armyndharise1901c3335d6
diff --git a/sdk/javascript/src/errors.ts b/sdk/javascript/src/errors.ts
index 7d1de872b..b72cf1457 100644
--- a/sdk/javascript/src/errors.ts
+++ b/sdk/javascript/src/errors.ts
@@ -66,7 +66,12 @@ export class OpenWAApiError extends OpenWAError {
);
}
let body: unknown = undefined;
- const text = await res.text().catch(() => '');
+ // An unreadable body leaves only the status to report, but an abort is the client timeout
+ // firing mid-read: rethrow it so the caller sees OpenWATimeoutError, not this status.
+ const text = await res.text().catch((err: unknown) => {
+ if (err instanceof Error && err.name === 'AbortError') throw err;
+ return '';
+ });
if (text) {
try {
body = JSON.parse(text);
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index e8d433bf4..838badcaa 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -339,6 +339,25 @@ describe('OpenWAClient', () => {
await expect(c.sessions.list()).rejects.toBeInstanceOf(OpenWATimeoutError);
});
+ it('reports a timeout, not the status, when a non-2xx response body stalls', async () => {
+ const stalledErrorFetch: FetchLike = async (_url, init) => {
+ const signal = init?.signal;
+ const body = new ReadableStream<Uint8Array>({
+ start(controller) {
+ signal?.addEventListener('abort', () => {
+ const error = new Error('body aborted');
+ error.name = 'AbortError';
+ controller.error(error);
+ });
+ },
+ });
+ return new Response(body, { status: 500 });
+ };
+ const c = new OpenWAClient({ baseUrl: 'http://x', apiKey: 'k', timeoutMs: 5, fetch: stalledErrorFetch });
+
+ await expect(c.sessions.list()).rejects.toBeInstanceOf(OpenWATimeoutError);
+ });
+
it('turns the timeout off for 0 or Infinity, and caps a delay setTimeout cannot hold', async () => {
// setTimeout fires after 1 ms for a delay that is not finite or exceeds 2^31-1, which would
// abort every request instead of waiting longer.