Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): answer reply and forward on an unresolved chat with 404

Yudhi Armyndharis9080944a90c6
diff --git a/src/engine/adapters/wwebjs-messaging-unknown-chat.spec.ts b/src/engine/adapters/wwebjs-messaging-unknown-chat.spec.ts
index 8693d547a..850601c04 100644
--- a/src/engine/adapters/wwebjs-messaging-unknown-chat.spec.ts
+++ b/src/engine/adapters/wwebjs-messaging-unknown-chat.spec.ts
@@ -6,7 +6,7 @@ import { MessageNotFoundError } from '../../common/errors/message-not-found.erro
/**
* getChatById RESOLVES undefined for a chat this account cannot see (whatsapp-web.js does not throw).
- * reactToMessage/getMessageReactions/deleteMessage then dereferenced it (`chat.fetchMessages`) with no
+ * reactToMessage/getMessageReactions/deleteMessage, and replyToMessage/forwardMessage, then dereferenced it (`chat.fetchMessages`) with no
* guard, so an unknown chatId produced a TypeError that withPage rethrew as an opaque 500, while the
* sibling editMessage returned the MessageNotFoundError (404) it deliberately throws for the same input.
* getChatHistory has no message to 404 on, so a chat with no accessible history yields an empty page.
@@ -37,6 +37,15 @@ describe('an unknown chat resolves to 404 (or an empty history), not a 500', ()
it('deleteMessage throws MessageNotFoundError', async () => {
await expect(makeMessaging().deleteMessage(CHAT, MESSAGE_ID)).rejects.toBeInstanceOf(MessageNotFoundError);
});
+ // A send-path 500 also counted toward the send breaker, which the caller's bad chat id must not trip.
+ it('replyToMessage throws MessageNotFoundError', async () => {
+ await expect(makeMessaging().replyToMessage(CHAT, MESSAGE_ID, 'x')).rejects.toBeInstanceOf(MessageNotFoundError);
+ });
+ it('forwardMessage throws MessageNotFoundError', async () => {
+ await expect(makeMessaging().forwardMessage(CHAT, '[email protected]', MESSAGE_ID)).rejects.toBeInstanceOf(
+ MessageNotFoundError,
+ );
+ });
it('getChatHistory returns an empty page rather than throwing', async () => {
await expect(makeMessaging().getChatHistory(CHAT, 10)).resolves.toEqual([]);
});
diff --git a/src/engine/adapters/wwebjs-messaging.ts b/src/engine/adapters/wwebjs-messaging.ts
index b3d68f68a..83af06347 100644
--- a/src/engine/adapters/wwebjs-messaging.ts
+++ b/src/engine/adapters/wwebjs-messaging.ts
@@ -541,6 +541,9 @@ export class WwebjsMessaging {
try {
// Find the message to quote
const chat = await this.client().getChatById(chatId);
+ if (!chat) {
+ throw new MessageNotFoundError(quotedMsgId, chatId);
+ }
const messages = await chat.fetchMessages({ limit: 100 });
const quotedMsg = messages.find(m => m.id._serialized === quotedMsgId);
@@ -569,6 +572,9 @@ export class WwebjsMessaging {
this.host.ensureReady();
try {
const chat = await this.client().getChatById(fromChatId);
+ if (!chat) {
+ throw new MessageNotFoundError(messageId, fromChatId);
+ }
const messages = await chat.fetchMessages({ limit: 100 });
const msgToForward = messages.find(m => m.id._serialized === messageId);