Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(engine): answer 404, not an opaque 500, when a wwjs group write gets a non-group id
Yudhi Armyndharis2169920da1de
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ca82f3fc6..480645cfc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Nine whatsapp-web.js group routes answer `404` (`GroupNotFoundError`) when the id is not a group or is unknown, like the guarded settings writes; they previously threw a bare error that surfaced as an opaque `500`.- docs/06 repair pass over the Errors lines: the ingress `401` is the signature failure (not an API-key error), label writes have no `404`, the catalog product lookup answers `200` empty (not `404`), search's `501` names the no-provider case, multi-line Errors blocks were re-joined (no severed sentences, duplicate codes, or `· ·` separators), and the gate now reads wrapped blocks.- docs/14 corrects four hazard-table release labels (instance-config is 0.18.0, the reload `409` is 0.15.0, the group-summary retypes are 0.14.6, Baileys 5xx is 0.14.5), docs/03 drops a duplicated `health/`, and docs/10's scaling note matches docs/13's "still deploy replicas: 1" stance.- docs/06's audit section scopes the always-null columns correctly (`userAgent`/`statusCode` always null; `method`/`path` populated on auth-failure, key-lifecycle and queue-board rows), the Chats quote box renders identically under system-dark and explicit dark, and the Logs empty state gives server-filter guidance when only the severity filter is active (13 locales).diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.tsindex 50281c637..b8898a6ea 100644--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts@@ -5591,6 +5591,24 @@ describe('WhatsAppWebJsAdapter group join + settings + own profile', () => {);});+ // The nine write routes that previously threw a bare Error('Chat is not a group') for a+ // non-group id (surfacing as an opaque 500) now share this guard: unknown id, 1:1 id and+ // status id are all GroupNotFoundError (404), like the guarded settings writes above.+ it.each([+ ['addParticipants', (a: WhatsAppWebJsAdapter) => a.addParticipants('[email protected]', ['[email protected]'])],+ ['removeParticipants', (a: WhatsAppWebJsAdapter) => a.removeParticipants('[email protected]', ['[email protected]'])],+ ['promoteParticipants', (a: WhatsAppWebJsAdapter) => a.promoteParticipants('[email protected]', ['[email protected]'])],+ ['demoteParticipants', (a: WhatsAppWebJsAdapter) => a.demoteParticipants('[email protected]', ['[email protected]'])],+ ['leaveGroup', (a: WhatsAppWebJsAdapter) => a.leaveGroup('[email protected]')],+ ['setGroupSubject', (a: WhatsAppWebJsAdapter) => a.setGroupSubject('[email protected]', 'New subject')],+ ['setGroupDescription', (a: WhatsAppWebJsAdapter) => a.setGroupDescription('[email protected]', 'New description')],+ ['getGroupInviteCode', (a: WhatsAppWebJsAdapter) => a.getGroupInviteCode('[email protected]')],+ ['revokeGroupInviteCode', (a: WhatsAppWebJsAdapter) => a.revokeGroupInviteCode('[email protected]')],+ ])('%s answers 404 (GroupNotFoundError) when the id is not a group', async (_name, call) => {+ const getChatById = jest.fn().mockResolvedValue({ isGroup: false });+ await expect(call(readyAdapter({ getChatById }))).rejects.toBeInstanceOf(GroupNotFoundError);+ });+it.each([['setGroupMessagesAdminsOnly', 'setMessagesAdminsOnly'],['setGroupInfoAdminsOnly', 'setInfoAdminsOnly'],diff --git a/src/engine/adapters/wwebjs-groups.ts b/src/engine/adapters/wwebjs-groups.tsindex 29d4bc0f2..8c2872b0c 100644--- a/src/engine/adapters/wwebjs-groups.ts+++ b/src/engine/adapters/wwebjs-groups.ts@@ -185,13 +185,9 @@ export class WwebjsGroups {}async addParticipants(groupId: string, participants: string[]): Promise<ParticipantOperationResult[]> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error('Chat is not a group');- }+ const chat = await this.requireGroupChat(groupId);const participantIds = participants.map(toParticipantWid);- const raw = await (chat as unknown as GroupChat).addParticipants(participantIds);+ const raw = await chat.addParticipants(participantIds);// whatsapp-web.js reports a batch-level refusal (no admin rights, empty group) by RESOLVING a// plain reason string (GroupChat.js:106-107,128-130) instead of throwing — surface it as a// refusal, not a success.@@ -248,13 +244,9 @@ export class WwebjsGroups {groupId: string,participants: string[],): Promise<ParticipantOperationResult[]> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error('Chat is not a group');- }+ const chat = await this.requireGroupChat(groupId);const participantIds = participants.map(toParticipantWid);- const res = await this.runParticipantBatch(op, groupId, chat as unknown as GroupChat, participantIds);+ const res = await this.runParticipantBatch(op, groupId, chat, participantIds);if (res?.status !== 200) {throw new EngineRefusedError(`${op} refused for group ${groupId} (status ${res?.status ?? 'unknown'})`);}@@ -319,51 +311,35 @@ export class WwebjsGroups {}async leaveGroup(groupId: string): Promise<void> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error('Chat is not a group');- }- await (chat as unknown as GroupChat).leave();+ const chat = await this.requireGroupChat(groupId);+ await chat.leave();}async setGroupSubject(groupId: string, subject: string): Promise<void> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error('Chat is not a group');- }+ const chat = await this.requireGroupChat(groupId);// GroupChat.setSubject resolves false when WA Web rejects the change (e.g. the account lacks// admin rights; index.d.ts:1982) instead of throwing — surface the refusal, not a false success.- const ok = await (chat as unknown as GroupChat).setSubject(subject);+ const ok = await chat.setSubject(subject);if (!ok) {throw new EngineRefusedError(`Failed to set the subject for group ${groupId} — admin rights required`);}}async setGroupDescription(groupId: string, description: string): Promise<void> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error('Chat is not a group');- }+ const chat = await this.requireGroupChat(groupId);// Same discarded-boolean contract as setSubject (index.d.ts:1984).- const ok = await (chat as unknown as GroupChat).setDescription(description);+ const ok = await chat.setDescription(description);if (!ok) {throw new EngineRefusedError(`Failed to set the description for group ${groupId} — admin rights required`);}}async getGroupInviteCode(groupId: string): Promise<string> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error(`${groupId} is not a group`);- }+ const chat = await this.requireGroupChat(groupId);// Typed Promise<string>, but WA Web yields nothing when the account is not an admin of the// group — and String(undefined) is the literal 'undefined', which the caller renders as the// link "https://chat.whatsapp.com/undefined". Same refusal contract as setDescription.- const inviteCode = await (chat as unknown as GroupChat).getInviteCode();+ const inviteCode = await chat.getInviteCode();if (!inviteCode) {throw new EngineRefusedError(`Failed to get the invite code for group ${groupId} — admin rights required`);}@@ -372,12 +348,8 @@ export class WwebjsGroups {}async revokeGroupInviteCode(groupId: string): Promise<string> {- this.host.ensureReady();- const chat = await this.client().getChatById(groupId);- if (!chat.isGroup) {- throw new Error(`${groupId} is not a group`);- }- const newCode = await (chat as unknown as GroupChat).revokeInvite();+ const chat = await this.requireGroupChat(groupId);+ const newCode = await chat.revokeInvite();if (!newCode) {throw new EngineRefusedError(`Failed to revoke the invite code for group ${groupId} — admin rights required`);}