fix: harden session error handling, normalizer null guard, extract redis-connection
- Wrap groupFetchAllParticipating in try/catch to prevent unhandled rejection on connect - Catch errors from async onMessage/onGroups callbacks via Promise.resolve().catch - Return null from normalizer when key.id is missing (prevents empty upsert key collision) - Extract parseRedisUrl to redis-connection.ts to eliminate duplication Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -69,4 +69,9 @@ describe('normalizeMessage', () => {
|
||||
const result = normalizeMessage({ message: { conversation: 'hi' } } as proto.IWebMessageInfo);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when message id is missing (prevents empty-key upsert collisions)', () => {
|
||||
const result = normalizeMessage(makeMsg({ key: { remoteJid: '120363043312345678@g.us', fromMe: false, id: undefined, participant: '919876543210@s.whatsapp.net' } }));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,10 +35,13 @@ export function normalizeMessage(msg: proto.IWebMessageInfo): NormalizedMessage
|
||||
|
||||
if (!msg.message) return null;
|
||||
|
||||
const platformMsgId = key.id;
|
||||
if (!platformMsgId) return null;
|
||||
|
||||
const content = extractText(msg);
|
||||
|
||||
return {
|
||||
platformMsgId: key.id ?? '',
|
||||
platformMsgId,
|
||||
sourceGroupJid: remoteJid,
|
||||
senderJid: key.participant ?? '',
|
||||
senderName: msg.pushName ?? undefined,
|
||||
|
||||
@@ -41,16 +41,24 @@ export async function createWhatsAppSession(
|
||||
setTimeout(() => createWhatsAppSession(sessionPath, onMessage, onGroups), 5000);
|
||||
}
|
||||
} else if (connection === 'open') {
|
||||
logger.info('WhatsApp connected');
|
||||
const groups = await sock.groupFetchAllParticipating();
|
||||
onGroups(groups);
|
||||
try {
|
||||
logger.info('WhatsApp connected');
|
||||
const groups = await sock.groupFetchAllParticipating();
|
||||
await Promise.resolve(onGroups(groups)).catch((err) =>
|
||||
logger.error({ err }, 'Group sync error'),
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ err }, 'Failed to fetch groups on connect');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
sock.ev.on('messages.upsert', ({ messages, type }) => {
|
||||
if (type !== 'notify') return;
|
||||
for (const msg of messages) {
|
||||
onMessage(msg);
|
||||
void Promise.resolve(onMessage(msg)).catch((err) =>
|
||||
logger.error({ err }, 'Error processing message'),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user