Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): answer 503 for a rate-limited Baileys channel lookup

Yudhi Armyndharis8c0f27623c2d
diff --git a/src/engine/adapters/baileys-channels.ts b/src/engine/adapters/baileys-channels.ts
index 8e6899548..bb85f28b2 100644
--- a/src/engine/adapters/baileys-channels.ts
+++ b/src/engine/adapters/baileys-channels.ts
@@ -1,6 +1,7 @@
import type { NewsletterMetadata, WASocket } from '@whiskeysockets/baileys';
import { Channel } from '../interfaces/whatsapp-engine.interface';
import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';
+import { EngineTransportError } from '../../common/errors/engine-transport.error';
import { mapServerRefusal } from './baileys-groups';
import { BAILEYS_QUERY_BUDGET_MS, withQueryDeadline } from './baileys-query-deadline';
@@ -111,14 +112,17 @@ export class BaileysChannels {
* A channel lookup that WhatsApp refuses (an unknown id, a bad invite code) comes back as a w:mex
* GraphQL error rather than an empty node, so a 4xx refusal is read as "no such channel", the same
* way the group invite lookup reads one. A rate limit (429) or a timeout (408) says nothing about
- * the channel, so it propagates, as does the deadline, which stays inside.
+ * the channel, so it answers 503, as the deadline does.
*/
private async lookup(type: 'jid' | 'invite', key: string, operation: string) {
try {
return await this.bounded(this.sock().newsletterMetadata(type, key), operation);
} catch (error) {
const code = wmexRefusalCode(error);
- if (code !== undefined && code >= 400 && code < 500 && code !== 408 && code !== 429) {
+ if (code === 408 || code === 429) {
+ throw new EngineTransportError(`WhatsApp rate-limited or timed out ${operation} (code ${code})`);
+ }
+ if (code !== undefined && code >= 400 && code < 500) {
return null;
}
throw error;
diff --git a/src/engine/adapters/baileys-wmex-refusal.spec.ts b/src/engine/adapters/baileys-wmex-refusal.spec.ts
index fe49332ea..d5b5e36bc 100644
--- a/src/engine/adapters/baileys-wmex-refusal.spec.ts
+++ b/src/engine/adapters/baileys-wmex-refusal.spec.ts
@@ -82,12 +82,22 @@ describe('a refused channel lookup is "no such channel", not a bare 500', () =>
}
});
- it("Baileys' own send timeout or a rate limit on the lookup propagates, not as 404", async () => {
+ it("Baileys' own send timeout on the lookup propagates, not as 404", async () => {
// promiseTimeout (Utils/generics.js) rejects a stalled send with an OBJECT data and a 408 code.
const timedOut = new Boom('Timed Out', { statusCode: 408, data: { stack: 'Error\n at x' } });
- for (const error of [timedOut, wmexRefusal(429)]) {
- const newsletterMetadata = jest.fn().mockRejectedValue(error);
- await expect(channels({ newsletterMetadata }, 500).getChannelById('120363@newsletter')).rejects.toBe(error);
+ const newsletterMetadata = jest.fn().mockRejectedValue(timedOut);
+ await expect(channels({ newsletterMetadata }, 500).getChannelById('120363@newsletter')).rejects.toBe(timedOut);
+ });
+
+ it('a lookup WhatsApp rate-limits or times out answers 503, not 404 or a bare 500', async () => {
+ for (const code of [408, 429]) {
+ const newsletterMetadata = jest.fn().mockRejectedValue(wmexRefusal(code));
+ await expect(channels({ newsletterMetadata }, 500).getChannelById('120363@newsletter')).rejects.toBeInstanceOf(
+ EngineTransportError,
+ );
+ const sock = { newsletterMetadata, newsletterFollow: jest.fn() };
+ await expect(channels(sock, 500).subscribeToChannel('INVITE')).rejects.toBeInstanceOf(EngineTransportError);
+ expect(sock.newsletterFollow).not.toHaveBeenCalled();
}
});