Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(groups): answer 503 when WhatsApp throttles the Baileys group list
Yudhi Armyndharis0fb3b8ffb6e5
diff --git a/src/engine/adapters/baileys-group-list.spec.ts b/src/engine/adapters/baileys-group-list.spec.tsindex 99bfd5164..4d1a9323d 100644--- a/src/engine/adapters/baileys-group-list.spec.ts+++ b/src/engine/adapters/baileys-group-list.spec.ts@@ -1,3 +1,4 @@+import { Boom } from '@hapi/boom';import type { WASocket } from '@whiskeysockets/baileys';import { BaileysGroups, BaileysGroupsHost } from './baileys-groups';import { EngineTransportError } from '../../common/errors/engine-transport.error';@@ -31,6 +32,17 @@ describe('getGroups', () => {await expect(groups({ groupFetchAllParticipating }, 15).getGroups()).rejects.toBeInstanceOf(EngineTransportError);});+ it.each([408, 429])('reports a WA %s as retryable instead of a bare 500', async code => {+ const groupFetchAllParticipating = jest.fn().mockRejectedValue(new Boom('rate-overlimit', { data: code }));+ await expect(groups({ groupFetchAllParticipating }, 500).getGroups()).rejects.toBeInstanceOf(EngineTransportError);+ });++ it('lets any other refusal propagate untouched', async () => {+ const err = new Boom('forbidden', { data: 403 });+ const groupFetchAllParticipating = jest.fn().mockRejectedValue(err);+ await expect(groups({ groupFetchAllParticipating }, 500).getGroups()).rejects.toBe(err);+ });+it('still returns an empty list when WhatsApp answers that there are no groups', async () => {// A real answer that happens to be empty: the library yields {} for this too, so the ONLY thing// separating it from the case above is that it arrived.diff --git a/src/engine/adapters/baileys-groups.ts b/src/engine/adapters/baileys-groups.tsindex c9c98e09d..370e5218a 100644--- a/src/engine/adapters/baileys-groups.ts+++ b/src/engine/adapters/baileys-groups.ts@@ -185,11 +185,22 @@ export class BaileysGroups {// groupFetchAllParticipating yields {} for BOTH an unanswered query and an account with no// groups, so the empty list carries no signal — only our own clock separates them, and an// empty list is the shape a caller is least able to question.- const all = await withQueryDeadline(- this.sock().groupFetchAllParticipating(),- this.queryBudgetMs,- 'WhatsApp did not answer the group list query in time',- );+ let all: Awaited<ReturnType<WASocket['groupFetchAllParticipating']>>;+ try {+ all = await withQueryDeadline(+ this.sock().groupFetchAllParticipating(),+ this.queryBudgetMs,+ 'WhatsApp did not answer the group list query in time',+ );+ } catch (err) {+ // A throttle is retryable, as on getGroupInfo. Any other refusal keeps its old shape: folding+ // it into mapServerRefusal's 403 would claim a permissions problem for a plain list read.+ const code = refusedStatusCode(err);+ if (code === 408 || code === 429) {+ throw new EngineTransportError(`WhatsApp rate-limited or timed out the group list query (code ${code})`);+ }+ throw err;+ }const self = this.host.normalizedSelfJid();return Object.values(all).map(metadata => mapBaileysGroup(metadata, self, jid => this.host.toNeutralJid(jid)));}