Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): stop a run on a 502 from a reverse proxy

Yudhi Armyndharis51a9b76dbd15
diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.ts
index faa4db172..ea21d80ca 100644
--- a/dashboard/src/pages/MessageTester.test.ts
+++ b/dashboard/src/pages/MessageTester.test.ts
@@ -340,6 +340,21 @@ test('a send answered without a message id fails that group and the run goes on'
assert.equal(rtl.screen.queryByText(/stopped/) === null, true);
});
+test('a 502 from a reverse proxy stops the run, since the gateway behind it is down', async () => {
+ const gateway = stubGroupGateway(
+ [
+ { id: '[email protected]', name: 'Family' },
+ { id: '[email protected]', name: 'Work' },
+ ],
+ 502,
+ );
+ await renderGroupsAsWriter();
+ await sendTextToAllGroups();
+
+ await rtl.screen.findByText('1, stopped after HTTP 502');
+ assert.deepEqual(gateway.textSends, ['[email protected]']);
+});
+
test('an empty message or a media type with no file or URL keeps Send disabled', async () => {
stubGroupGateway([
{ id: '[email protected]', name: 'Family' },
diff --git a/dashboard/src/pages/NumberChecker.test.ts b/dashboard/src/pages/NumberChecker.test.ts
index 0cbc6dbf4..4b7b25bd0 100644
--- a/dashboard/src/pages/NumberChecker.test.ts
+++ b/dashboard/src/pages/NumberChecker.test.ts
@@ -902,12 +902,13 @@ test('NumberChecker: a gateway refusal stops the batch and the numbers never rea
const STOP_REASON: Record<number, RegExp> = {
400: /The session is not connected \(HTTP 400\)/,
+ 502: /The gateway refused a lookup \(HTTP 502\)/,
409: /The session is not connected \(HTTP 409\)/,
429: /The gateway rate limit was reached \(HTTP 429\)/,
503: /WhatsApp did not answer a lookup \(HTTP 503\)/,
};
-for (const status of [400, 409, 429, 503]) {
+for (const status of [400, 409, 429, 502, 503]) {
test(`NumberChecker: HTTP ${status} on the first lookup stops the batch before the second`, async () => {
const lookups = stubLookups(() =>
jsonResponse({ message: status === 400 ? 'Session is not started' : `refused ${status}` }, status),
diff --git a/dashboard/src/utils/groupSelection.test.ts b/dashboard/src/utils/groupSelection.test.ts
index 12fc9b9fa..e31c420bd 100644
--- a/dashboard/src/utils/groupSelection.test.ts
+++ b/dashboard/src/utils/groupSelection.test.ts
@@ -82,10 +82,14 @@ test('a stopped session, a refusal and a fetch with no answer end a run, other f
const withStatus = (status: number, message = 'refused') => Object.assign(new Error(message), { status });
assert.equal(isGatewayRefusal(withStatus(409)), true);
assert.equal(isGatewayRefusal(withStatus(429)), true);
+ // A reverse proxy answers 502 for a gateway it cannot reach, and 503 as the gateway does.
+ assert.equal(isGatewayRefusal(withStatus(502)), 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);
+ // A 504 means the proxy reached the gateway and stopped waiting, so the request may still be running.
+ assert.equal(isGatewayRefusal(withStatus(504)), false);
// 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);
diff --git a/dashboard/src/utils/groupSelection.ts b/dashboard/src/utils/groupSelection.ts
index b8ca057b7..32811b654 100644
--- a/dashboard/src/utils/groupSelection.ts
+++ b/dashboard/src/utils/groupSelection.ts
@@ -11,9 +11,11 @@ export interface GroupPickerRow {
export type GroupSendPlan = { mode: 'single'; chatId: string } | { mode: 'sequential'; chatIds: string[] };
// 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];
+// 429 the rate limit, 502 a reverse proxy that cannot reach the gateway, 503 WhatsApp not answering. A 504 is left out:
+// the proxy reached the gateway and stopped waiting, so that request may still be running. 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, 502, 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.