Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): keep a refused catalog lookup out of the send breaker
Yudhi Armyndharis12374b018c08
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex c3834433d..055e667bd 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -152,7 +152,7 @@ jest.mock('@whiskeysockets/baileys', () => ({}));import { HttpsProxyAgent } from 'https-proxy-agent';-import { NotFoundException, BadRequestException } from '@nestjs/common';+import { NotFoundException, BadRequestException, ForbiddenException } from '@nestjs/common';import { SocksProxyAgent } from 'socks-proxy-agent';import { Dispatcher1Wrapper } from 'undici';import { BaileysAdapter, createProxyAgent } from './baileys.adapter';@@ -176,6 +176,7 @@ import { ChatLabelsUnsupportedError } from '../../common/errors/chat-labels-unsuimport { Boom } from '@hapi/boom';import { EngineTransportError } from '../../common/errors/engine-transport.error';import { LidNotMappedError } from '../../common/errors/lid-not-mapped.error';+import { countsTowardSendBreaker } from '../../modules/message/send-pacing.service';import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';import * as safeLinkPreview from './safe-link-preview';@@ -7352,6 +7353,18 @@ describe('BaileysAdapter catalog (#905)', () => {await expect(adapter.sendProduct('[email protected]', 'p1')).rejects.toThrow(BadRequestException);expect(fakeSock.sendMessage).not.toHaveBeenCalled();});++ // The breaker measures WhatsApp refusing this account's sends; a catalog read it refused is not one.+ it('sendProduct answers 403 for a refused catalog lookup without feeding the send breaker', async () => {+ const adapter = await ready();+ fakeSock.getCatalog.mockRejectedValue(new Boom('refused', { data: 403 }));++ const error: unknown = await adapter.sendProduct('[email protected]', 'p1').catch((e: unknown) => e);++ expect(error).toBeInstanceOf(ForbiddenException);+ expect(countsTowardSendBreaker(error)).toBe(false);+ expect(fakeSock.sendMessage).not.toHaveBeenCalled();+ });});// Baileys models the reachout timelock first-class — a typed state with both a push notification anddiff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.tsindex a1f41f622..20d074146 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -49,7 +49,8 @@ import {StatusPostOptions,} from '../interfaces/whatsapp-engine.interface';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';-import { NotFoundException } from '@nestjs/common';+import { EngineRefusedError } from '../../common/errors/engine-refused.error';+import { ForbiddenException, NotFoundException } from '@nestjs/common';import { createLogger } from '../../common/services/logger.service';import { BaileysAdapterConfig } from '../types/baileys.types';import { baileysAuthDir } from '../auth-dir-paths';@@ -753,7 +754,12 @@ export class BaileysAdapter implements IWhatsAppEngine {return this.catalog.getProduct(productId);}async sendProduct(chatId: string, productId: string, body?: string): Promise<MessageResult> {- const product = await this.catalog.getProduct(productId);+ const product = await this.catalog.getProduct(productId).catch((error: unknown) => {+ // Still 403, but no longer an EngineRefusedError: the send breaker counts WhatsApp refusing a+ // send, and a catalog read it refused says nothing about the account's send standing.+ if (error instanceof EngineRefusedError) throw new ForbiddenException(error.message);+ throw error;+ });if (!product) {throw new NotFoundException(`Product ${productId} not found in the session catalog`);}