Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-js): show a non-envelope error body instead of [object Object]

Yudhi Armyndharis1dff3169ef07
diff --git a/sdk/javascript/src/errors.ts b/sdk/javascript/src/errors.ts
index 72b82b5a4..296b41f49 100644
--- a/sdk/javascript/src/errors.ts
+++ b/sdk/javascript/src/errors.ts
@@ -149,5 +149,8 @@ function isNestEnvelope(body: unknown): body is NestErrorEnvelope {
function describeMessage(message: string | string[] | unknown): string {
if (Array.isArray(message)) return message.join(', ');
if (typeof message === 'string') return message;
+ // A body without the envelope, such as the readiness 503's `{ status, details }`. It came from
+ // JSON.parse, so it always stringifies.
+ if (typeof message === 'object' && message !== null) return JSON.stringify(message);
return String(message);
}
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index f70337a3a..15f573f01 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -114,6 +114,21 @@ describe('OpenWAClient', () => {
);
});
+ it('renders a non-envelope error body as JSON in the message', async () => {
+ // The readiness probe answers 503 with `{ status, details }`, which has no `statusCode`/`message`.
+ const t = new MockTransport().on('GET', '/api/health/ready', {
+ status: 503,
+ body: { status: 'error', details: { mainDatabase: { status: 'down' } } },
+ });
+ const err = await client(t)
+ .health.ready()
+ .catch((e: unknown) => e);
+
+ expect(err).toBeInstanceOf(OpenWAServiceUnavailableError);
+ expect((err as OpenWAApiError).message).toContain('"mainDatabase":{"status":"down"}');
+ expect((err as OpenWAApiError).message).not.toContain('[object Object]');
+ });
+
it('exposes all expected resource properties', () => {
const c = client(new MockTransport());
for (const r of [