Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
feat(hooks): log a refused message:sending reply so the operator can find it
Yudhi Armyndharis60f1879ad596
diff --git a/src/core/hooks/sending-gate.spec.ts b/src/core/hooks/sending-gate.spec.tsindex 471b8293c..8bdce4a1e 100644--- a/src/core/hooks/sending-gate.spec.ts+++ b/src/core/hooks/sending-gate.spec.ts@@ -1,6 +1,7 @@import { BadRequestException } from '@nestjs/common';import { applySendingGate } from './sending-gate';import { HookManager } from './hook-manager.service';+import { LoggerService } from '../../common/services/logger.service';/*** The gate returned `(hookData as { input: T }).input` with no check that the hook chain's reply@@ -44,4 +45,36 @@ describe('applySendingGate', () => {await expect(gate(result)).rejects.toThrow(BadRequestException);await expect(gate(result)).rejects.toThrow(/message:sending/);});++ /**+ * The 400 goes to the API caller, who is not the person who can fix it: a plugin returning a bad+ * envelope stops EVERY send on that session, and the operator's only evidence was a client-side+ * error they never see. The refusal has to leave a trace on the server, carrying the session and+ * the call site so the failing session is identifiable.+ */+ describe('leaves the operator a trace', () => {+ let warn: jest.SpyInstance;+ beforeEach(() => {+ warn = jest.spyOn(LoggerService.prototype, 'warn').mockImplementation(() => undefined);+ });+ afterEach(() => warn.mockRestore());++ it('warns, naming the session and the call site, when the envelope is unusable', async () => {+ await expect(gate({ continue: true, data: { notInput: 1 } })).rejects.toThrow(BadRequestException);++ expect(warn).toHaveBeenCalledTimes(1);+ const [message, context] = warn.mock.calls[0] as [string, Record<string, unknown>];+ expect(message).toMatch(/message:sending/);+ expect(context).toMatchObject({ sessionId: 's1', source: 'MessageService', type: 'text' });+ });++ // Negative twin: the ordinary paths must stay quiet, or the signal is worthless.+ it.each([+ ['an intact envelope', { continue: true, data: { input: { chatId: '[email protected]', text: 'hi' } } }],+ ['no data at all', { continue: true }],+ ])('stays silent on %s', async (_label, result) => {+ await gate(result);+ expect(warn).not.toHaveBeenCalled();+ });+ });});diff --git a/src/core/hooks/sending-gate.ts b/src/core/hooks/sending-gate.tsindex 718859acf..daef22d4d 100644--- a/src/core/hooks/sending-gate.ts+++ b/src/core/hooks/sending-gate.ts@@ -1,5 +1,8 @@import { BadRequestException } from '@nestjs/common';import { HookManager } from './hook-manager.service';+import { createLogger } from '../../common/services/logger.service';++const logger = createLogger('SendingGate');/*** Run the pre-send `message:sending` plugin gate for one piece of outbound content and return the@@ -41,8 +44,9 @@ export async function applySendingGate<T extends object>(if (!shouldContinue) {throw new BadRequestException('Message sending blocked by plugin');}- // A chain that replies with nothing changed nothing: keep the caller's own input. This is the- // ordinary no-plugin path and must not be treated as a malformed reply.+ // No data at all means nothing in the chain rewrote the input, so keep the caller's own. This+ // covers both the ordinary no-plugin path and a handler that deliberately returned nothing;+ // neither is a malformed reply.if (hookData === undefined) {return input;}@@ -61,6 +65,15 @@ export async function applySendingGate<T extends object>(typeof envelope.input !== 'object' ||envelope.input === null) {+ // The 400 reaches the API caller, who cannot fix a plugin. This refusal stops EVERY send on the+ // session, so the operator needs it in the server log — with the session and call site, since+ // the chain's aggregated reply does not say which handler produced it.+ logger.warn('A message:sending handler returned a payload without a usable `input`; refusing the send', {+ sessionId,+ source,+ type,+ received: hookData === null ? 'null' : typeof hookData,+ });throw new BadRequestException('A message:sending handler returned a payload without a usable `input`; the send was refused rather than sent unmoderated',);