Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwebjs): answer an unknown label id with 404 instead of 500
Yudhi Armyndharisb192893bcc96
diff --git a/docs/29-engine-capability-matrix.md b/docs/29-engine-capability-matrix.mdindex b9d33ea4b..0301fb57a 100644--- a/docs/29-engine-capability-matrix.md+++ b/docs/29-engine-capability-matrix.md@@ -722,8 +722,8 @@ with zero OpenWA surface. Baileys-only; whatsapp-web.js has no community API at| `addOrRemoveLabels` | ✅ `addLabelToChat`, `removeLabelFromChat` || `getChatLabels` | ❌ **not exposed** — the adapter reads the chat and calls `Chat.getLabels()`, not the Client method || `getChatsByLabelId` | ✅ `getChatsByLabel` |-| `getLabelById` | ✅ `getLabelById` |-| `getLabels` | ✅ `getLabels` |+| `getLabelById` | ❌ **not exposed**: it throws page-side for an unknown id, so the adapter picks from `getLabels` |+| `getLabels` | ✅ `getLabels`, `getLabelById` |**Status & broadcasts** (3)@@ -984,8 +984,8 @@ adapter sources — re-derive the same way when anything changes:**9** wwjs-only; `sendCatalog` (unavailable on both engines) is not exposed.- Full engine inventory (29.5), split by the exposure legend rather than lumped: Baileys **152**socket methods — 48 wired into interface methods, 5 internal wiring, 29 plumbing, **70 ❌ not- exposed** (incl. the whole 23-method community cluster); wwjs **81** Client methods — 43 wired,- 3 internal wiring, 1 class plumbing, **34 ❌ not exposed** (26 real capabilities + 8+ exposed** (incl. the whole 23-method community cluster); wwjs **81** Client methods — 42 wired,+ 3 internal wiring, 1 class plumbing, **35 ❌ not exposed** (27 real capabilities + 8session/transport settings that are not WhatsApp capabilities). The backlog is the ❌ rows minusthose 8 settings; 🔩 plumbing is correctly never exposed.- Events: Baileys **34** (16 consumed / 18 dropped), wwjs **31** (16 consumed / 15 dropped).diff --git a/src/engine/adapters/wwebjs-label-transport-death.spec.ts b/src/engine/adapters/wwebjs-label-transport-death.spec.tsindex 87973a90c..c1c655b99 100644--- a/src/engine/adapters/wwebjs-label-transport-death.spec.ts+++ b/src/engine/adapters/wwebjs-label-transport-death.spec.ts@@ -20,8 +20,11 @@ describe('label operations distinguish a dead page from an ordinary failure', ()op === name ? jest.fn().mockRejectedValue(reject) : jest.fn().mockResolvedValue([]);const chat = { getLabels: rejects('getChatLabels') };const client = {- getLabels: rejects('getLabels'),- getLabelById: op === 'getLabelById' ? jest.fn().mockRejectedValue(reject) : jest.fn().mockResolvedValue(null),+ // getLabelById reads the full list: Client.getLabelById throws page-side for an unknown id.+ getLabels:+ op === 'getLabels' || op === 'getLabelById'+ ? jest.fn().mockRejectedValue(reject)+ : jest.fn().mockResolvedValue([]),getChatById: jest.fn().mockResolvedValue(chat),addOrRemoveLabels:op === 'changeChatLabel' ? jest.fn().mockRejectedValue(reject) : jest.fn().mockResolvedValue(undefined),@@ -72,4 +75,23 @@ describe('label operations distinguish a dead page from an ordinary failure', ()await expect(labels.addLabelToChat('[email protected]', '7')).rejects.toThrow(/business/i);});++ // Client.getLabelById never resolves null for an unknown id (its page code serializes undefined),+ // so the adapter picks from the list; a missing id, or any id on a personal account, is null (404).+ it('getLabelById resolves a listed label and null for an unknown one', async () => {+ const client = { getLabels: jest.fn().mockResolvedValue([{ id: '5', name: 'Paid', hexColor: '#25D366' }]) };+ const host = {+ ensureReady: jest.fn(),+ getClient: () => client as unknown as Client,+ isPageTransportError: () => false,+ reportIfPageTransportError: jest.fn(),+ logger,+ } as unknown as WwebjsEngineHost;+ const labels = new WwebjsLabels(host);++ await expect(labels.getLabelById('5')).resolves.toEqual({ id: '5', name: 'Paid', hexColor: '#25D366' });+ await expect(labels.getLabelById('999')).resolves.toBeNull();+ client.getLabels.mockResolvedValue([]);+ await expect(labels.getLabelById('5')).resolves.toBeNull();+ });});diff --git a/src/engine/adapters/wwebjs-labels.ts b/src/engine/adapters/wwebjs-labels.tsindex a2287e7ba..b74d6f8a6 100644--- a/src/engine/adapters/wwebjs-labels.ts+++ b/src/engine/adapters/wwebjs-labels.ts@@ -92,9 +92,13 @@ export class WwebjsLabels {async getLabelById(labelId: string): Promise<Label | null> {this.host.ensureReady();- const label = await withPage(this.host, 'getLabelById', () =>- (this.client() as unknown as BusinessClient).getLabelById(labelId),+ // Client.getLabelById never resolves null: its page code serializes the looked-up label without+ // checking it exists, so an unknown id (every id on a personal account) throws a TypeError that+ // surfaced as a 500. Picking from the full list makes a missing label the documented 404.+ const labels = await withPage(this.host, 'getLabelById', () =>+ (this.client() as unknown as BusinessClient).getLabels(),);+ const label = labels?.find(candidate => String(candidate.id) === labelId);if (!label) {return null;}diff --git a/src/engine/engine-capability-matrix.ts b/src/engine/engine-capability-matrix.tsindex 28f54b928..7f58ba22b 100644--- a/src/engine/engine-capability-matrix.ts+++ b/src/engine/engine-capability-matrix.ts@@ -218,7 +218,7 @@ export const CURATED_CAPABILITY_EXCEPTIONS: Record<string, MethodCapability> = {wwjs: { status: 'supported' },baileys: { status: 'not-available', rootCause: 'library-limitation' },evidence:- 'baileys no getLabel/getLabelById in lib/**/*.d.ts (Types/Label.d.ts has only Label interface + LabelColor enum + LabelActionBody); derivable only from an app-state-sync label cache; wwjs Client.getLabelById (Client.js:2838)',+ 'baileys no getLabel/getLabelById in lib/**/*.d.ts (Types/Label.d.ts has only Label interface + LabelColor enum + LabelActionBody); derivable only from an app-state-sync label cache; wwjs picks from Client.getLabels (Client.js:2760), since Client.getLabelById (Client.js:2838) throws page-side for an unknown id',},getLabels: {wwjs: { status: 'supported' },diff --git a/src/engine/types/whatsapp-web-js.types.ts b/src/engine/types/whatsapp-web-js.types.tsindex 3151ab6e9..c1110d996 100644--- a/src/engine/types/whatsapp-web-js.types.ts+++ b/src/engine/types/whatsapp-web-js.types.ts@@ -121,14 +121,12 @@ export interface BusinessClient extends Omit<| 'subscribeToChannel'| 'unsubscribeFromChannel'| 'getLabels'- | 'getLabelById'| 'getChannels'| 'getChatsByLabelId'| 'createChannel'| 'deleteChannel'> {getLabels(): Promise<Array<{ id: string; name: string; hexColor: string }>>;- getLabelById(id: string): Promise<{ id: string; name: string; hexColor: string } | null>;/** Chats carrying a label. whatsapp-web.js has the read but exposes no label create/update/delete. */getChatsByLabelId(labelId: string): Promise<Array<