Skip to content

Commit 48b6de0

Browse files
Merge pull request #232 from contentstack/mkt-8235
Mkt 8235
2 parents fbb96cf + 7b88b06 commit 48b6de0

File tree

4 files changed

+110
-62
lines changed

4 files changed

+110
-62
lines changed

fetch.js

Lines changed: 80 additions & 52 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-source-contentstack",
3-
"version": "5.3.2",
3+
"version": "5.3.3",
44
"description": "Gatsby source plugin for building websites using Contentstack as a data source",
55
"scripts": {
66
"prepublish": "npm run build",

src/fetch.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const getData = async (url, options) => {
168168
});
169169
};
170170

171-
const fetchCsData = async (url, config, query) => {
171+
const fetchCsData = async (url, config, query, SyncRetryCount = 0) => {
172172
query = query || {};
173173
query.include_count = true;
174174
query.environment = config.environment;
@@ -296,12 +296,32 @@ const getSyncData = async (
296296
* To make final sync call and concatenate the result if found any during on fetch request.
297297
*/
298298
const aggregatedSyncToken = syncToken.filter(item => item !== undefined);
299-
for (const token of aggregatedSyncToken) {
300-
const syncResponse = await fetchCsData(
301-
url,
302-
config,
303-
(query = { sync_token: token })
304-
);
299+
for (const token of aggregatedSyncToken) {
300+
301+
let syncResponse;
302+
try {
303+
304+
syncResponse = await fetchCsData(
305+
url,
306+
config,
307+
(query = { sync_token: token }),
308+
0 // Reset SyncRetryCount for each call
309+
);
310+
} catch (error) {
311+
if (SyncRetryCount < config.httpRetries) {
312+
const timeToWait = 2 ** SyncRetryCount * 100;
313+
//Retry attempt ${retries + 1} after sync token error. Waiting for ${timeToWait} ms...
314+
await waitFor(timeToWait);
315+
return syncResponse = await fetchCsData(
316+
url,
317+
config,
318+
(query = { sync_token: token }),
319+
SyncRetryCount + 1
320+
);
321+
} else {
322+
throw new Error(`Failed to fetch sync data after ${config.httpRetries} retry attempts due to invalid sync token.`);
323+
}
324+
}
305325
aggregatedResponse.data = aggregatedResponse.data?.concat(
306326
...syncResponse.items
307327
);

0 commit comments

Comments
 (0)