Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): tell an unparseable payload by its error class

Yudhi Armyndharis9df2ae222fc8
diff --git a/src/modules/integration/ingress-reconciler.service.spec.ts b/src/modules/integration/ingress-reconciler.service.spec.ts
index c5f54f7b5..6777867ac 100644
--- a/src/modules/integration/ingress-reconciler.service.spec.ts
+++ b/src/modules/integration/ingress-reconciler.service.spec.ts
@@ -172,6 +172,24 @@ describe('IngressReconcilerService.sweep', () => {
expect(third.skipped).toBe(OPTS.batchSize);
});
+ it('sets aside a row whose payload is text that reads like a connection error', async () => {
+ // The SyntaxError of JSON.parse quotes the start of the text, so by its message alone this row would
+ // be taken for a database that said "not now" and kept at the head of every sweep.
+ const unreadable: string[] = [];
+ for (let i = 0; i < OPTS.batchSize; i++) unreadable.push(await insertEvent({ createdAt: minutesAgo(30) }));
+ const healthy = await insertEvent();
+ for (const id of unreadable) {
+ await ds.query(`UPDATE ingress_events SET payload = 'connection reset by peer' WHERE id = ?`, [id]);
+ }
+
+ const first = await service.sweep(OPTS);
+ expect(first).toMatchObject({ replayed: 0, skipped: OPTS.batchSize });
+ const second = await service.sweep(OPTS);
+
+ expect(second).toMatchObject({ replayed: 1, failed: 0 });
+ expect((await stored(healthy)).dispatchState).toBe('dispatched');
+ });
+
it('reads a row again on the next sweep when the read failed because the database was busy', async () => {
// Not a row that cannot be read: a lock or a dropped connection says nothing about its payload, and the
// delivery is one the reconciler exists to replay within minutes.
diff --git a/src/modules/integration/ingress-reconciler.service.ts b/src/modules/integration/ingress-reconciler.service.ts
index 7e952a0a5..4732905be 100644
--- a/src/modules/integration/ingress-reconciler.service.ts
+++ b/src/modules/integration/ingress-reconciler.service.ts
@@ -192,7 +192,11 @@ export class IngressReconcilerService implements OnModuleInit, OnModuleDestroy {
});
// Set aside only a row whose payload could not be read. A read that failed because the database
// said "not now" (a lock, a dropped connection) is looked at again by the next sweep, as before.
- if (!isTransientDbError(err)) this.rememberUnreadable(id, now);
+ // A payload that does not parse is told by its error class: the message of a SyntaxError quotes
+ // the start of the text, which may read like a connection error.
+ if ((err instanceof Error && err.name === 'SyntaxError') || !isTransientDbError(err)) {
+ this.rememberUnreadable(id, now);
+ }
stats.skipped++;
continue;
}