Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): fail the contact load when a later page stays throttled

Yudhi Armyndharis35db67e6d461
diff --git a/dashboard/src/services/api.test.ts b/dashboard/src/services/api.test.ts
index 9bf8f1245..33a859bbe 100644
--- a/dashboard/src/services/api.test.ts
+++ b/dashboard/src/services/api.test.ts
@@ -103,3 +103,26 @@ test('the contact list is not cut off at 10,000 contacts', async () => {
assert.equal((await contactApi.list('s1')).length, 11_005);
});
+
+test('the contact list rejects instead of returning a partial list when a later page stays throttled', async () => {
+ const { contactApi } = await import('./api.ts');
+ globalThis.fetch = ((input: RequestInfo | URL) => {
+ if (!String(input).includes('offset=0')) {
+ return Promise.resolve(
+ new Response(JSON.stringify({ statusCode: 429, message: 'Too Many Requests' }), {
+ status: 429,
+ headers: { 'Content-Type': 'application/json' },
+ }),
+ );
+ }
+ const contacts = Array.from({ length: 1000 }, (_, i) => ({ id: `${i}@c.us`, name: null }));
+ return Promise.resolve(
+ new Response(JSON.stringify(contacts), { status: 200, headers: { 'Content-Type': 'application/json' } }),
+ );
+ }) as typeof fetch;
+
+ await assert.rejects(
+ contactApi.list('s1'),
+ (err: Error & { status?: number }) => err.status === 429 && err.message === 'Too Many Requests',
+ );
+});
diff --git a/dashboard/src/services/api.ts b/dashboard/src/services/api.ts
index 0522dc573..b9176cc08 100644
--- a/dashboard/src/services/api.ts
+++ b/dashboard/src/services/api.ts
@@ -973,17 +973,26 @@ export interface ProfilePictureResponse {
export const contactApi = {
// The route caps a response at 1000 contacts; walk the pages so an address book past that is complete.
// No item cap: the status recipient picker needs every contact, and the server's short page ends the walk.
- list: async (sessionId: string) =>
- (
- await fetchAllPages(
- async (limit, offset) => {
+ list: async (sessionId: string) => {
+ let lastError: unknown;
+ const { items, throttled } = await fetchAllPages(
+ async (limit, offset) => {
+ try {
const data = await request<Contact[]>(`/sessions/${sessionId}/contacts?limit=${limit}&offset=${offset}`);
// The route answers a bare array with no total: a short page is the last one.
return { data, total: data.length < limit ? offset + data.length : Infinity };
- },
- { pageSize: 1000, maxItems: Infinity },
- )
- ).items,
+ } catch (err) {
+ lastError = err;
+ throw err;
+ }
+ },
+ { pageSize: 1000, maxItems: Infinity },
+ );
+ // A page still throttled after the retries would leave the picker silently short; fail the whole
+ // load with that page's 429, as a throttled first page already does.
+ if (throttled) throw lastError;
+ return items;
+ },
checkNumber: (sessionId: string, number: string) =>
request<CheckNumberResponse>(`/sessions/${sessionId}/contacts/check/${encodeURIComponent(number)}`),
// Returns the contact/group profile picture URL. Both engines return null when the user hid their