Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwebjs): name a page rejection that is not an error
Yudhi Armyndharis248277d5c8d4
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex 6a4208c8c..aa3e9b1a3 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -5936,6 +5936,25 @@ describe('WhatsAppWebJsAdapter inbound media measured in the page', () => {compilerOptions: options,}).outputText;// Declared alongside whatever helpers the compiler hoisted, then sent as its own text, as puppeteer does.+ // WhatsApp Web rejects with more than errors: a bare string, or an object that carries only a status+ // (upstream reads e.status === 404), and the operator reads the warning that names it.+ it.each([+ ['a bare string', 'download refused', 'download refused (WhatsApp Web 2.3000.1)'],+ ['an object with only a status', { status: 500 }, 'status 500 (WhatsApp Web 2.3000.1)'],+ [+ 'an object with a name and a status',+ { name: 'MediaError', status: 410 },+ 'MediaError: status 410 (WhatsApp Web 2.3000.1)',+ ],+ ['nothing at all', undefined, 'unknown rejection (WhatsApp Web 2.3000.1)'],+ ])('names a rejection that is %s', async (_label, rejection, expected) => {+ // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors+ pageServing(5, { decrypt: () => Promise.reject(rejection) });+ (g.window as { Debug?: unknown }).Debug = { VERSION: '2.3000.1' };++ await expect(downloadMediaInPage({ msgId: 'M1', maxBytes: 1000 })).rejects.toThrow(expected);+ });+const declared = vm.runInNewContext(`${compiled}\n;downloadMediaInPage`) as { toString(): string };return vm.runInNewContext(`(${declared.toString()})`, {window: (globalThis as unknown as { window: unknown }).window,@@ -6001,7 +6020,7 @@ describe('WhatsAppWebJsAdapter inbound media measured in the page', () => {);expect(literals(own)).toEqual(literals(upstream));// The copy measures the decrypted length instead of reporting the declared `size`, and names a- // rejection from its `name`, `message` and the page's `Debug.VERSION`.+ // rejection from its `name`, class, `message`, `status` and the page's `Debug.VERSION`.expect(members(own)).toEqual([...members(upstream).filter(m => m !== 'size'),@@ -6029,6 +6048,7 @@ describe('WhatsAppWebJsAdapter inbound media measured in the page', () => {* into one value (`Client.js:1558`), and its typings claim `Promise<Message>` — so nothing upstream of* these tests catches a regression here.*/+ 'constructor',describe('WhatsAppWebJsAdapter send-result contract', () => {const readyAdapter = (client: unknown): WhatsAppWebJsAdapter => {const adapter = new WhatsAppWebJsAdapter({ sessionId: 's', sessionDataPath: './data/sessions', puppeteer: {} });diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.tsindex db570515c..48e9e6fe3 100644--- a/src/engine/adapters/wwebjs-messaging.ts+++ b/src/engine/adapters/wwebjs-messaging.ts@@ -160,10 +160,22 @@ export async function downloadMediaInPage(args: {} catch (e) {if ((e as { status?: number } | null)?.status === 404) return undefined;// WhatsApp Web's rejection classes are minified, and puppeteer rebuilds a page exception from the class- // name alone (`t: t`). Name the rejection here, with the build it came from.- const thrown = e as { name?: unknown; message?: unknown } | null;+ // name alone (`t: t`). Name the rejection here, with the build it came from, from whatever it carries:+ // a name and message, the class and a status, or a bare string.+ const thrown = e as+ { name?: string; message?: string; status?: number; constructor?: { name?: string } } | string | null | undefined;+ const part = (value: string | number | null | undefined): string =>+ value === undefined || value === null ? '' : String(value);+ let summary: string;+ if (thrown instanceof Object) {+ const name = part(thrown.name) || (thrown.constructor === Object ? '' : part(thrown.constructor?.name));+ const reason = part(thrown.message) || (thrown.status === undefined ? '' : `status ${part(thrown.status)}`);+ summary = name && reason ? `${name}: ${reason}` : name || reason;+ } else {+ summary = part(thrown);+ }const build = (window as unknown as { Debug?: { VERSION?: unknown } }).Debug?.VERSION;- throw new Error(`${String(thrown?.name)}: ${String(thrown?.message)} (WhatsApp Web ${String(build)})`, {+ throw new Error(`${summary || 'unknown rejection'} (WhatsApp Web ${String(build)})`, {cause: e,});}