@@ -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