Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): report a dropped Baileys picture lookup as 503, not null
Yudhi Armyndharis0d04af4cd44c
diff --git a/src/engine/adapters/baileys-contacts.ts b/src/engine/adapters/baileys-contacts.tsindex b72b91d03..502894a0a 100644--- a/src/engine/adapters/baileys-contacts.ts+++ b/src/engine/adapters/baileys-contacts.ts@@ -8,6 +8,7 @@ import { RecipientUnreachableError } from '../../common/errors/recipient-unreachimport { LidNotMappedError } from '../../common/errors/lid-not-mapped.error';import { parseWaId, userPart } from '../identity/wa-id';import { storedKeyInChat } from './baileys-message-mapper';+import { refusedStatusCode } from './baileys-groups';/*** Contacts/profile/chats-domain operations extracted from BaileysAdapter. The adapter keeps the@@ -91,6 +92,11 @@ export class BaileysContacts {if (err instanceof EngineTransportError) {throw err;}+ // Only WhatsApp's own error answer (a numeric code: 404 item-not-found, 401 not-authorized) is+ // that verdict. A socket that closed mid-lookup carries none and must not read as "no picture".+ if (refusedStatusCode(err) === undefined) {+ throw new EngineTransportError('WhatsApp did not answer the profile picture lookup');+ }this.host.logger.debug('profilePictureUrl failed; no picture or hidden', {contactId,error: err instanceof Error ? err.message : String(err),diff --git a/src/engine/adapters/baileys-profile-picture.spec.ts b/src/engine/adapters/baileys-profile-picture.spec.tsindex d7de3b3ea..864701f65 100644--- a/src/engine/adapters/baileys-profile-picture.spec.ts+++ b/src/engine/adapters/baileys-profile-picture.spec.ts@@ -1,3 +1,4 @@+import { Boom } from '@hapi/boom';import type { WASocket } from '@whiskeysockets/baileys';import { BaileysContacts, BaileysContactsHost } from './baileys-contacts';import { EngineTransportError } from '../../common/errors/engine-transport.error';@@ -36,10 +37,18 @@ describe('getProfilePicture', () => {});it('still answers null when the library reports no picture', async () => {- const profilePictureUrl = jest.fn().mockRejectedValue(new Error('item-not-found'));+ // assertNodeErrorFree's verdict: a Boom carrying WhatsApp's numeric code.+ const profilePictureUrl = jest.fn().mockRejectedValue(new Boom('item-not-found', { data: 404 }));await expect(contacts({ profilePictureUrl }, 500).getProfilePicture('[email protected]')).resolves.toBeNull();});+ it('reports a connection that closed mid-lookup instead of "no picture"', async () => {+ const profilePictureUrl = jest.fn().mockRejectedValue(new Boom('Connection Closed', { statusCode: 428 }));+ await expect(contacts({ profilePictureUrl }, 500).getProfilePicture('[email protected]')).rejects.toBeInstanceOf(+ EngineTransportError,+ );+ });+it('still answers null when the library resolves nothing', async () => {const profilePictureUrl = jest.fn().mockResolvedValue(undefined);await expect(contacts({ profilePictureUrl }, 500).getProfilePicture('[email protected]')).resolves.toBeNull();diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.tsindex b393613bc..a62b7edc8 100644--- a/src/engine/adapters/baileys.adapter.spec.ts+++ b/src/engine/adapters/baileys.adapter.spec.ts@@ -6212,7 +6212,7 @@ describe('BaileysAdapter profile + block', () => {const adapter = await ready();expect(await adapter.getProfilePicture('[email protected]')).toBe('https://pps/x.jpg');expect(fakeSock.profilePictureUrl).toHaveBeenCalledWith('[email protected]', 'image');- fakeSock.profilePictureUrl.mockRejectedValueOnce(new Error('no picture'));+ fakeSock.profilePictureUrl.mockRejectedValueOnce(new Boom('item-not-found', { data: 404 }));expect(await adapter.getProfilePicture('[email protected]')).toBeNull();});