Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions __tests__/services/zendesk-api-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,44 @@ describe("ZendeskService", () => {
});
expect(result).toEqual(tags);
});

it("should handle pagination limit error gracefully and return partial results", async () => {
const tags1 = [{ name: "tag1" }];
const tags2 = [{ name: "tag2" }];
const paginationError = {
responseJSON: {
error: "InvalidPaginationDepth"
}
};

requestMock
.mockResolvedValueOnce({ tags: tags1, next_page: "next_page" })
.mockResolvedValueOnce({ tags: tags2, next_page: "page_101" })
.mockRejectedValueOnce(paginationError);

const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();

const result = await service.getTags();

expect(requestMock).toHaveBeenCalledTimes(3);
expect(result).toEqual([...tags1, ...tags2]);
expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining("Reached pagination limit")
);

consoleWarnSpy.mockRestore();
});

it("should re-throw non-pagination errors", async () => {
const tags = [{ name: "tag1" }];
const otherError = new Error("Network error");

requestMock
.mockResolvedValueOnce({ tags, next_page: "next_page" })
.mockRejectedValueOnce(otherError);

await expect(service.getTags()).rejects.toThrow("Network error");
});
});

describe("getGroups", () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zendesk/zaf-toolbox",
"version": "1.6.0",
"version": "1.6.1",
"description": "A toolbox for ZAF application built with 🩷 by Zendesk Labs",
"main": "lib/src/index.js",
"types": "lib/src/index.d.ts",
Expand Down
31 changes: 26 additions & 5 deletions src/services/zendesk-api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,32 @@ export class ZendeskApiService {
while (true) {
const nextPage = results[results.length - 1].next_page;
if (!nextPage) break;
results.push(
await this.client.request<TResponse>({
url: nextPage
})
);
try {
results.push(
await this.client.request<TResponse>({
url: nextPage
})
);
} catch (error: unknown) {
// Stop gracefully on pagination limit error (Zendesk API limits to page 100 or 10,000 records)
if (
error &&
typeof error === "object" &&
"responseJSON" in error &&
error.responseJSON &&
typeof error.responseJSON === "object" &&
"error" in error.responseJSON &&
typeof error.responseJSON.error === "string" &&
error.responseJSON.error.includes("InvalidPaginationDepth")
) {
console.warn(
`Reached pagination limit for ${nextPage} (page 100 / ~10,000 records). Returning partial results.`
);
break;
}
// Re-throw all other errors
throw error;
}
}
}

Expand Down
Loading