Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): return 404 (not 500) for reply/forward/react/delete on a missing message (#331)

Yudhi Armyndharisa6a50e15e743
diff --git a/src/common/errors/message-not-found.error.spec.ts b/src/common/errors/message-not-found.error.spec.ts
new file mode 100644
index 000000000..4a806c4aa
--- /dev/null
+++ b/src/common/errors/message-not-found.error.spec.ts
@@ -0,0 +1,20 @@
+import { NotFoundException } from '@nestjs/common';
+import { MessageNotFoundError } from './message-not-found.error';
+
+// F-16 — reply/forward/react/delete on a message outside the adapter's fetch/store window threw a
+// raw Error -> HTTP 500. MessageNotFoundError extends NotFoundException so NestJS maps it to 404
+// through the built-in handler (no global filter), surviving the message.service passthrough.
+describe('MessageNotFoundError (F-16)', () => {
+ it('is a NotFoundException -> HTTP 404 without a custom global filter', () => {
+ const err = new MessageNotFoundError('ABC');
+ expect(err).toBeInstanceOf(NotFoundException);
+ expect(err.getStatus()).toBe(404);
+ });
+
+ it('formats the message, optionally including the chat id', () => {
+ expect(new MessageNotFoundError('ABC').message).toBe('Message ABC not found');
+ expect(new MessageNotFoundError('ABC', '[email protected]').message).toBe(
+ 'Message ABC not found in chat [email protected]',
+ );
+ });
+});
diff --git a/src/common/errors/message-not-found.error.ts b/src/common/errors/message-not-found.error.ts
new file mode 100644
index 000000000..78385a33a
--- /dev/null
+++ b/src/common/errors/message-not-found.error.ts
@@ -0,0 +1,17 @@
+import { NotFoundException } from '@nestjs/common';
+
+/**
+ * Thrown by the engine layer when a message referenced by id can't be found — it is outside the
+ * adapter's fetch/store window (e.g. older than the ~100-message lookup, or absent from the Baileys
+ * store) or was revoked.
+ *
+ * Extends NestJS `NotFoundException` so it maps to **HTTP 404** through the built-in exception
+ * handler — i.e. it does NOT depend on a custom global filter being registered, and it survives the
+ * `message.service` passthrough (and the un-wrapped react/delete paths) — instead of surfacing as a
+ * generic 500 Internal Server Error.
+ */
+export class MessageNotFoundError extends NotFoundException {
+ constructor(messageId: string, chatId?: string) {
+ super(chatId ? `Message ${messageId} not found in chat ${chatId}` : `Message ${messageId} not found`);
+ }
+}
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index 1bab1fee5..452c7c8ad 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -36,6 +36,7 @@ import {
import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';
import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';
import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';
+import { MessageNotFoundError } from '../../common/errors/message-not-found.error';
import { createLogger } from '../../common/services/logger.service';
import { BaileysAdapterConfig, BaileysLogger } from '../types/baileys.types';
import { BaileysSessionStore } from './baileys-session-store';
@@ -933,7 +934,7 @@ export class BaileysAdapter implements IWhatsAppEngine {
private async requireStored(messageId: string): Promise<WAMessage> {
const found = await this.config.messageStore?.getMessage(this.config.sessionId, messageId);
if (!found?.key) {
- throw new Error(`Message ${messageId} not found`);
+ throw new MessageNotFoundError(messageId);
}
return found;
}
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.ts
index 57fe366ae..4c940c3d4 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts
@@ -34,6 +34,7 @@ import {
} from '../interfaces/whatsapp-engine.interface';
import { createLogger } from '../../common/services/logger.service';
import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';
+import { MessageNotFoundError } from '../../common/errors/message-not-found.error';
import { assertSafeFetchUrl } from '../../common/security/ssrf-guard';
import {
GroupChat,
@@ -678,7 +679,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
const quotedMsg = messages.find(m => m.id._serialized === quotedMsgId);
if (!quotedMsg) {
- throw new Error(`Message ${quotedMsgId} not found`);
+ throw new MessageNotFoundError(quotedMsgId);
}
const msg = await quotedMsg.reply(text);
@@ -695,7 +696,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
const msgToForward = messages.find(m => m.id._serialized === messageId);
if (!msgToForward) {
- throw new Error(`Message ${messageId} not found`);
+ throw new MessageNotFoundError(messageId);
}
await msgToForward.forward(toChatId);
@@ -829,7 +830,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
const messages = await chat.fetchMessages({ limit: 100 });
const message = messages.find(m => m.id._serialized === messageId);
if (!message) {
- throw new Error(`Message ${messageId} not found in chat ${chatId}`);
+ throw new MessageNotFoundError(messageId, chatId);
}
await (message as MessageWithReactions).react(emoji);
this.logger.log(`Reacted to message ${messageId} with ${emoji || '(removed)'}`);
@@ -841,7 +842,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
const messages = await chat.fetchMessages({ limit: 100 });
const message = messages.find(m => m.id._serialized === messageId);
if (!message) {
- throw new Error(`Message ${messageId} not found in chat ${chatId}`);
+ throw new MessageNotFoundError(messageId, chatId);
}
const msgWithReactions = message as MessageWithReactions;
if (!msgWithReactions.hasReaction) {
@@ -1048,7 +1049,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
const messages = await chat.fetchMessages({ limit: 100 });
const message = messages.find(m => m.id._serialized === messageId || m.id.id === messageId);
if (!message) {
- throw new Error(`Message ${messageId} not found in chat ${chatId}`);
+ throw new MessageNotFoundError(messageId, chatId);
}
await message.delete(forEveryone);
this.logger.log(`Deleted message ${messageId} from chat ${chatId} (forEveryone: ${forEveryone})`);