Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): keep failing the contact read when nothing was readable

Yudhi Armyndharis36d7959c1b51
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index e1d30951b..7fa5ee0d4 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -6062,6 +6062,18 @@ describe('WhatsAppWebJsAdapter page transport error detection (wedged page fast-
expect(onDisconnected).not.toHaveBeenCalled();
});
+ // When no model at all could be read, the page is broken as a whole; an empty 200 would read as an
+ // address book with every contact deleted, so the failure still reaches the caller.
+ it('fails the read when WhatsApp Web could not read any contact', async () => {
+ const evaluate = jest.fn().mockResolvedValue({ rows: [], failed: 3, firstError: 'x is not a function' });
+ const { adapter, onDisconnected } = readyAdapter({ pupPage: { evaluate } });
+
+ await expect(adapter.getContacts()).rejects.toThrow(
+ 'WhatsApp Web could not read any of 3 contact(s): x is not a function',
+ );
+ expect(onDisconnected).not.toHaveBeenCalled();
+ });
+
// A rejection that carries no transport-death signature is an ordinary failure, not a dead page —
// it must reach the caller unchanged and leave the session READY, unlike the 503 case above.
it('propagates a non-transport rejection from getContacts untouched and leaves the session READY', async () => {
diff --git a/src/engine/adapters/wwebjs-contacts.ts b/src/engine/adapters/wwebjs-contacts.ts
index b90457a4e..f4197bf57 100644
--- a/src/engine/adapters/wwebjs-contacts.ts
+++ b/src/engine/adapters/wwebjs-contacts.ts
@@ -143,6 +143,13 @@ export class WwebjsContacts {
throw error;
}
+ // Every model threw: a systemic page failure (a renamed WA Web module, WWebJS not injected), not
+ // one unreadable entry. Answer an error, as before the per-entry catch, rather than an empty
+ // address book that a syncing consumer would read as every contact deleted.
+ if (raw.length === 0 && unreadable > 0) {
+ throw new Error(`WhatsApp Web could not read any of ${unreadable} contact(s): ${firstError}`);
+ }
+
// Read every id through readWid (so the `_serialized`->`$1` rename lands here too) and skip
// entries with no readable id, counted and logged like the chats path, rather than dropping
// the whole address book silently.