Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwjs): report a dead page from mute/pin as transport, not as a bad chatId
Yudhi Armyndharisda099d202aa7
diff --git a/src/engine/adapters/chat-mute.spec.ts b/src/engine/adapters/chat-mute.spec.tsindex 309f26278..5d9e6b7d9 100644--- a/src/engine/adapters/chat-mute.spec.ts+++ b/src/engine/adapters/chat-mute.spec.ts@@ -39,6 +39,11 @@ describe('WwebjsChats.muteChat', () => {ensureReady: jest.fn(),getClient: () => client as unknown as Client,logger,+ // Both are declared on WwebjsEngineHost and called unguarded by every operation in this+ // adapter; a stub that omits them is a host shape production never has, and it hid which+ // failures this file classifies as transport.+ isPageTransportError: () => false,+ reportIfPageTransportError: jest.fn(),} as unknown as WwebjsEngineHost;return { chats: new WwebjsChats(host, {} as unknown as WwebjsMessaging), client };}@@ -108,6 +113,48 @@ describe('WwebjsChats.muteChat', () => {await expect(chats.muteChat('[email protected]', MUTE_UNTIL)).rejects.toThrow('findImpl');await expect(chats.muteChat('[email protected]', MUTE_UNTIL)).rejects.not.toMatchObject({ status: 400 });});++ /**+ * The chat resolution swallowed EVERY rejection into "no such chat", so a dead page — the state+ * where nothing can be resolved — was reported to the caller as a 400 naming their chatId. Five+ * other operations in this adapter split that case out and answer 503; leaving these two behind+ * made the same file answer a dead page two different ways, and told a caller to fix a request+ * that was never the problem.+ */+ describe.each([+ ['muteChat', (c: WwebjsChats) => c.muteChat('[email protected]', MUTE_UNTIL)],+ ['pinChat', (c: WwebjsChats) => c.pinChat('[email protected]', true)],+ ])('%s reports a dead page as transport, not as a bad chatId', (_name, call) => {+ it('rethrows EngineTransportError', async () => {+ const dead = new Error('Execution context was destroyed');+ const client = {+ getChatById: jest.fn().mockRejectedValue(dead),+ muteChat: jest.fn(),+ unmuteChat: jest.fn(),+ pinChat: jest.fn(),+ unpinChat: jest.fn(),+ };+ const reportIfPageTransportError = jest.fn();+ const host = {+ ensureReady: jest.fn(),+ getClient: () => client as unknown as Client,+ logger,+ isPageTransportError: (error: unknown) => error === dead,+ reportIfPageTransportError,+ } as unknown as WwebjsEngineHost;+ const chats = new WwebjsChats(host, {} as unknown as WwebjsMessaging);++ await expect(call(chats)).rejects.toBeInstanceOf(EngineTransportError);+ expect(reportIfPageTransportError).toHaveBeenCalled();+ });+ });++ // Negative twin: an ordinary unresolvable chat must still be the 400 the routes document.+ it('still answers 400 when the page simply does not know the chat', async () => {+ const { chats, client } = makeChats();+ client.getChatById.mockResolvedValue(undefined);+ await expect(chats.muteChat('[email protected]', MUTE_UNTIL)).rejects.toMatchObject({ status: 400 });+ });});describe('BaileysContacts.muteChat', () => {diff --git a/src/engine/adapters/chat-pin.spec.ts b/src/engine/adapters/chat-pin.spec.tsindex 8901360d0..a52f0440c 100644--- a/src/engine/adapters/chat-pin.spec.ts+++ b/src/engine/adapters/chat-pin.spec.ts@@ -40,6 +40,10 @@ describe('WwebjsChats.pinChat', () => {ensureReady: jest.fn(),getClient: () => client as unknown as Client,logger,+ // Declared on WwebjsEngineHost and called unguarded throughout this adapter; a stub without+ // them is a host shape production never has.+ isPageTransportError: () => false,+ reportIfPageTransportError: jest.fn(),} as unknown as WwebjsEngineHost;return { chats: new WwebjsChats(host, {} as unknown as WwebjsMessaging), client };}diff --git a/src/engine/adapters/wwebjs-chats.ts b/src/engine/adapters/wwebjs-chats.tsindex 9e2372e79..81338d0f6 100644--- a/src/engine/adapters/wwebjs-chats.ts+++ b/src/engine/adapters/wwebjs-chats.ts@@ -155,9 +155,19 @@ export class WwebjsChats {* by resolving undefined or by rejecting.*/private async requireResolvableChat(chatId: string): Promise<void> {- const chat = await this.client()- .getChatById(chatId)- .catch(() => undefined);+ let chat: unknown;+ try {+ chat = await this.client().getChatById(chatId);+ } catch (error) {+ // A dead page resolves NOTHING, so swallowing every rejection here reported it as "no such+ // chat" and named the caller's chatId as the problem. Five operations in this adapter already+ // split that case out; without this, the same file answered a dead page two different ways.+ if (this.host.isPageTransportError(error)) {+ this.host.reportIfPageTransportError(error, 'requireResolvableChat');+ throw new EngineTransportError('Transport died while resolving a chat');+ }+ chat = undefined; // an unknown chat rejects too — that is the 400 below+ }if (!chat) throw new BadRequestException(`Chat ${chatId} does not exist on this session`);}