Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): stop a run on a 400 only for a stopped session

Yudhi Armyndhariseb39c9822b72
diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.ts
index b5ee86e46..faa4db172 100644
--- a/dashboard/src/pages/MessageTester.test.ts
+++ b/dashboard/src/pages/MessageTester.test.ts
@@ -145,7 +145,7 @@ function groupJsonResponse(data: unknown, status = 200): Response {
}
// How the first text send fails: an HTTP status, a fetch that never gets an answer, or a 201 with no message id.
-type FirstSendFailure = number | 'unreachable' | 'unconfirmed';
+type FirstSendFailure = number | 'unreachable' | 'unconfirmed' | 'vetoed';
function stubGroupGateway(
groups: { id: string; name?: string }[],
@@ -173,6 +173,10 @@ function stubGroupGateway(
if (refuseFirstWith && textSends.length === 1) {
if (refuseFirstWith === 'unreachable') return Promise.reject(new TypeError('Failed to fetch'));
if (refuseFirstWith === 'unconfirmed') return Promise.resolve(groupJsonResponse({ timestamp: 1 }, 201));
+ // A plugin that vetoes one chat's send: the gateway answers 400 for that chat alone.
+ if (refuseFirstWith === 'vetoed') {
+ return Promise.resolve(groupJsonResponse({ message: 'Message sending blocked by plugin' }, 400));
+ }
const message =
refuseFirstWith === 400 ? "Session 's1' is not active. Start the session first." : 'Too many requests';
return Promise.resolve(groupJsonResponse({ message }, refuseFirstWith));
@@ -283,6 +287,25 @@ test('a 400 (the session is not active) stops the run, since every group would f
assert.deepEqual(gateway.textSends, ['[email protected]']);
});
+// The same 400 status, but about one chat: that group fails by itself and the others are still sent.
+test('a 400 that blocks one group fails that group and the run goes on', async () => {
+ const gateway = stubGroupGateway(
+ [
+ { id: '[email protected]', name: 'Family' },
+ { id: '[email protected]', name: 'Work' },
+ ],
+ 'vetoed',
+ );
+ await renderGroupsAsWriter();
+ await sendTextToAllGroups();
+
+ // The run waits 3 seconds between groups, so the second send comes after that.
+ await rtl.screen.findByText('1/2 sent', {}, { timeout: 8000 });
+ assert.deepEqual(gateway.textSends, ['[email protected]', '[email protected]']);
+ assert.ok(rtl.screen.getByText(/blocked by plugin/, { selector: 'li' }));
+ assert.equal(rtl.screen.queryByText(/stopped/) === null, true);
+});
+
test('a gateway that does not answer stops the run, and the notice says so', async () => {
const gateway = stubGroupGateway(
[
diff --git a/dashboard/src/pages/NumberChecker.test.ts b/dashboard/src/pages/NumberChecker.test.ts
index 618483cc9..0cbc6dbf4 100644
--- a/dashboard/src/pages/NumberChecker.test.ts
+++ b/dashboard/src/pages/NumberChecker.test.ts
@@ -909,7 +909,9 @@ const STOP_REASON: Record<number, RegExp> = {
for (const status of [400, 409, 429, 503]) {
test(`NumberChecker: HTTP ${status} on the first lookup stops the batch before the second`, async () => {
- const lookups = stubLookups(() => jsonResponse({ message: `refused ${status}` }, status));
+ const lookups = stubLookups(() =>
+ jsonResponse({ message: status === 400 ? 'Session is not started' : `refused ${status}` }, status),
+ );
const { container } = renderComponent();
const textarea = await openBulkTab();
rtl.fireEvent.change(textarea, { target: { value: '911111111111\n912222222222' } });
@@ -963,6 +965,22 @@ test('NumberChecker: an answer that is not JSON fails that number and the run go
assert.equal(container.querySelector('.nc-batch-stopped'), null);
});
+// 400 ends a run only for a session that is not running; any other 400 is about that number.
+test('NumberChecker: a 400 that is not about the session fails that number and the run goes on', async () => {
+ const lookups = stubLookups(number =>
+ number === '911111111111' ? jsonResponse({ message: 'Invalid number' }, 400) : registered(number),
+ );
+ const { container } = renderComponent();
+ const textarea = await openBulkTab();
+ rtl.fireEvent.change(textarea, { target: { value: '911111111111\n912222222222' } });
+ startBatch();
+
+ await rtl.screen.findByText('Completed checking 2 numbers.', {}, { timeout: 3000 });
+ assert.deepEqual(lookups, ['911111111111', '912222222222']);
+ assert.equal(statValue('Failed'), '1');
+ assert.equal(container.querySelector('.nc-batch-stopped'), null);
+});
+
test('NumberChecker: a refusal on the last number says so instead of counting numbers left unsent', async () => {
const lookups = stubLookups(number =>
number === '912222222222' ? jsonResponse({ message: 'Rate limit exceeded (HTTP 429)' }, 429) : registered(number),
diff --git a/dashboard/src/utils/groupSelection.test.ts b/dashboard/src/utils/groupSelection.test.ts
index a19bf590f..12fc9b9fa 100644
--- a/dashboard/src/utils/groupSelection.test.ts
+++ b/dashboard/src/utils/groupSelection.test.ts
@@ -79,16 +79,28 @@ test('forward uses the first group as its source chat instead of looping', () =>
});
test('a stopped session, a refusal and a fetch with no answer end a run, other failures do not', () => {
- const withStatus = (status: number) => Object.assign(new Error('refused'), { status });
- assert.equal(isGatewayRefusal(withStatus(400)), true);
+ const withStatus = (status: number, message = 'refused') => Object.assign(new Error(message), { status });
assert.equal(isGatewayRefusal(withStatus(409)), true);
assert.equal(isGatewayRefusal(withStatus(429)), true);
assert.equal(isGatewayRefusal(withStatus(503)), true);
assert.equal(isGatewayRefusal(new TypeError('Failed to fetch')), true);
assert.equal(isGatewayRefusal(withStatus(404)), false);
assert.equal(isGatewayRefusal(withStatus(500)), false);
- // An error the page raised itself, or an unreadable answer, is not a fetch that got no answer.
+ // An error the page raised itself, or an answer it could not read, is not a fetch that got no answer.
assert.equal(isGatewayRefusal(new Error('offline')), false);
assert.equal(isGatewayRefusal(new SyntaxError('Unexpected token')), false);
assert.equal(isGatewayRefusal(null), false);
});
+
+// A 400 is also what the gateway answers for one chat alone, so it ends a run only when it says the session is not running.
+test('a 400 ends a run only when it says the session is not active or not started', () => {
+ const withStatus = (message: string) => Object.assign(new Error(message), { status: 400 });
+ assert.equal(isGatewayRefusal(withStatus("Session 's1' is not active. Start the session first.")), true);
+ assert.equal(isGatewayRefusal(withStatus('Session is not started')), true);
+ assert.equal(isGatewayRefusal(withStatus('SESSION IS NOT STARTED')), true);
+ assert.equal(isGatewayRefusal(withStatus('Message sending blocked by plugin')), false);
+ assert.equal(isGatewayRefusal(withStatus('The body failed validation')), false);
+ assert.equal(isGatewayRefusal(withStatus('Contact is not active on WhatsApp')), false);
+ assert.equal(isGatewayRefusal(withStatus('')), false);
+ assert.equal(isGatewayRefusal(Object.assign(new Error('Session is not started'), { status: 404 })), false);
+});
diff --git a/dashboard/src/utils/groupSelection.ts b/dashboard/src/utils/groupSelection.ts
index 96ab8aa6f..b8ca057b7 100644
--- a/dashboard/src/utils/groupSelection.ts
+++ b/dashboard/src/utils/groupSelection.ts
@@ -10,9 +10,14 @@ export interface GroupPickerRow {
export type GroupSendPlan = { mode: 'single'; chatId: string } | { mode: 'sequential'; chatIds: string[] };
-// Answers the next call of the same run gets too: 400 a session with no engine (the send and lookup routes answer it),
-// 409 an engine that is not ready, which fails every group alike, 429 the rate limit, 503 WhatsApp not answering.
-const GATEWAY_REFUSAL_STATUSES: readonly number[] = [400, 409, 429, 503];
+// Answers the next call of the same run gets too: 409 an engine that is not ready, which fails every group alike,
+// 429 the rate limit, 503 WhatsApp not answering. A 400 is not listed because the gateway also answers it for one chat
+// alone (a send a plugin vetoed); it counts only when it says the session is not running, see SESSION_NOT_RUNNING.
+const GATEWAY_REFUSAL_STATUSES: readonly number[] = [409, 429, 503];
+
+// How the gateway words a 400 for a session with no engine: "Session '<id>' is not active. Start the session first."
+// on the send routes, "Session is not started" on the contact and group routes.
+const SESSION_NOT_RUNNING = /\bsession\b.*\bnot (active|started)\b/i;
export function groupLabel(group: SelectableGroup): string {
return group.name?.trim() || group.id;
@@ -54,10 +59,11 @@ export function planGroupSend(selectedIds: readonly string[], messageType: strin
}
// A failure the rest of a run would meet too, so the run stops instead of recording one failure per remaining target:
-// one of the statuses above, or a fetch that got no answer at all (it rejects with a TypeError). An error the page
-// raised itself, or an answer it could not read, fails that call only.
+// one of the statuses above, a 400 that says the session is not running, or a fetch that got no answer at all (it
+// rejects with a TypeError). An error the page raised itself, or an answer it could not read, fails that call only.
export function isGatewayRefusal(err: unknown): boolean {
if (err instanceof TypeError) return true;
- const status = (err as { status?: unknown } | null)?.status;
+ const { status, message } = (err ?? {}) as { status?: unknown; message?: unknown };
+ if (status === 400) return typeof message === 'string' && SESSION_NOT_RUNNING.test(message);
return typeof status === 'number' && GATEWAY_REFUSAL_STATUSES.includes(status);
}