Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(catalog): hand back the pacing hold when a product lookup times out
Yudhi Armyndharis89916e04794d
diff --git a/src/common/errors/engine-not-sent.error.ts b/src/common/errors/engine-not-sent.error.tsnew file mode 100644index 000000000..da7ba4505--- /dev/null+++ b/src/common/errors/engine-not-sent.error.ts@@ -0,0 +1,9 @@+import { EngineTransportError } from './engine-transport.error';++/**+ * An {@link EngineTransportError} (HTTP 503) for a send that failed in transport before the message was+ * handed to WhatsApp, such as a lookup the send depends on timing out. Like a throttled request, nothing+ * went out, while a plain EngineTransportError leaves the outcome unknown, so a paced send can give its+ * admission back.+ */+export class EngineNotSentError extends EngineTransportError {}diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex 055e667bd..94b46a391 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, ForbiddenException } from '@nestjs/common';+import { NotFoundException, BadRequestException, ForbiddenException, HttpException } from '@nestjs/common';import { SocksProxyAgent } from 'socks-proxy-agent';import { Dispatcher1Wrapper } from 'undici';import { BaileysAdapter, createProxyAgent } from './baileys.adapter';@@ -176,7 +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 { countsTowardSendBreaker, sentNothing } from '../../modules/message/send-pacing.service';import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';import * as safeLinkPreview from './safe-link-preview';@@ -7365,6 +7365,18 @@ describe('BaileysAdapter catalog (#905)', () => {expect(countsTowardSendBreaker(error)).toBe(false);expect(fakeSock.sendMessage).not.toHaveBeenCalled();});++ // The message was never handed to WhatsApp, so a paced send gives its admission back.+ it('sendProduct answers 503 as nothing sent when the catalog lookup times out', async () => {+ const adapter = await ready();+ fakeSock.getCatalog.mockRejectedValue(new Boom('timed out', { data: 408 }));++ const error: unknown = await adapter.sendProduct('[email protected]', 'p1').catch((e: unknown) => e);++ expect((error as HttpException).getStatus()).toBe(503);+ expect(sentNothing(error)).toBe(true);+ 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 20d074146..cd67c21d0 100644--- a/src/engine/adapters/baileys.adapter.ts+++ b/src/engine/adapters/baileys.adapter.ts@@ -50,6 +50,8 @@ import {} from '../interfaces/whatsapp-engine.interface';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';import { EngineRefusedError } from '../../common/errors/engine-refused.error';+import { EngineNotSentError } from '../../common/errors/engine-not-sent.error';+import { EngineTransportError } from '../../common/errors/engine-transport.error';import { ForbiddenException, NotFoundException } from '@nestjs/common';import { createLogger } from '../../common/services/logger.service';import { BaileysAdapterConfig } from '../types/baileys.types';@@ -758,6 +760,8 @@ export class BaileysAdapter implements IWhatsAppEngine {// 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);+ // A lookup that timed out or was throttled failed before the message was handed to WhatsApp.+ if (error instanceof EngineTransportError) throw new EngineNotSentError(error.message);throw error;});if (!product) {diff --git a/src/modules/message/send-pacing.service.ts b/src/modules/message/send-pacing.service.tsindex 48d3928b8..2a64150e8 100644--- a/src/modules/message/send-pacing.service.ts+++ b/src/modules/message/send-pacing.service.ts@@ -10,6 +10,7 @@ import { createLogger } from '../../common/services/logger.service';import { EngineRefusedError } from '../../common/errors/engine-refused.error';import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';import { EngineThrottledError } from '../../common/errors/engine-throttled.error';+import { EngineNotSentError } from '../../common/errors/engine-not-sent.error';import { SsrfBlockedError } from '../../common/security/ssrf-guard';import { AuditService } from '../audit/audit.service';import { AuditAction } from '../audit/entities/audit-log.entity';@@ -57,15 +58,19 @@ export function countsTowardSendBreaker(error: unknown): boolean {/*** Whether a send that threw provably never went out, so its admission can be handed back: a client or* refusal status (4xx), a 501 for something the engine cannot do, a media URL the SSRF guard blocked- * before any fetch, or a WhatsApp rate limit (EngineThrottledError), turned away before it ran. Anything- * else (a deadline, a dropped socket, a dead page) leaves the outcome unknown; WhatsApp may have taken- * the message, so its admission stays held.+ * before any fetch, a WhatsApp rate limit (EngineThrottledError), turned away before it ran, or a transport+ * failure before the message was handed to WhatsApp (EngineNotSentError). Anything else (a deadline, a+ * dropped socket, a dead page) leaves the outcome unknown; WhatsApp may have taken the message, so its+ * admission stays held.*/export function sentNothing(error: unknown): boolean {return (error instanceof SsrfBlockedError ||(error instanceof HttpException &&- (error.getStatus() < 500 || error instanceof EngineNotSupportedError || error instanceof EngineThrottledError))+ (error.getStatus() < 500 ||+ error instanceof EngineNotSupportedError ||+ error instanceof EngineThrottledError ||+ error instanceof EngineNotSentError)));}