@contentstack/datasync-manager 2.3.0 → 2.4.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/api.js CHANGED
@@ -158,6 +158,41 @@ const get = (req, RETRY = 1) => {
158
158
  }, timeDelay);
159
159
  }
160
160
  else {
161
+ // Enhanced error handling for Error 141 (Invalid sync_token)
162
+ try {
163
+ const errorBody = JSON.parse(body);
164
+ // Validate error response structure and check for Error 141
165
+ if (errorBody && typeof errorBody === 'object' && errorBody.error_code === 141 && errorBody.errors && typeof errorBody.errors === 'object' && errorBody.errors.sync_token) {
166
+ debug('Error 141 detected: Invalid sync_token. Triggering auto-recovery with init=true');
167
+ // Ensure req.qs exists before modifying
168
+ if (!req.qs) {
169
+ req.qs = {};
170
+ }
171
+ // Clear the invalid token parameters and reinitialize
172
+ if (req.qs.sync_token) {
173
+ delete req.qs.sync_token;
174
+ }
175
+ if (req.qs.pagination_token) {
176
+ delete req.qs.pagination_token;
177
+ }
178
+ req.qs.init = true;
179
+ // Mark this as a recovery attempt to prevent infinite loops
180
+ if (!req._error141Recovery) {
181
+ req._error141Recovery = true;
182
+ debug('Retrying with init=true after Error 141');
183
+ return (0, exports.get)(req, 1) // Reset retry counter for fresh start
184
+ .then(resolve)
185
+ .catch(reject);
186
+ }
187
+ else {
188
+ debug('Error 141 recovery already attempted, failing to prevent infinite loop');
189
+ }
190
+ }
191
+ }
192
+ catch (parseError) {
193
+ // Body is not JSON or parsing failed, continue with normal error handling
194
+ debug('Error response parsing failed:', parseError);
195
+ }
161
196
  debug(messages_1.MESSAGES.API.REQUEST_FAILED(options));
162
197
  return reject(body);
163
198
  }
@@ -361,6 +361,20 @@ const fire = (req) => {
361
361
  .catch(reject);
362
362
  }).catch((error) => {
363
363
  debug(messages_1.MESSAGES.SYNC_CORE.ERROR_FIRE, error);
364
+ // Check if this is an Error 141 (invalid token) - enhanced handling
365
+ try {
366
+ const parsedError = typeof error === 'string' ? JSON.parse(error) : error;
367
+ if (parsedError.error_code === 141) {
368
+ logger_1.logger.error('Error 141: Invalid sync_token detected. Token has been reset.');
369
+ logger_1.logger.info('System will automatically re-initialize with fresh token on next sync.');
370
+ // The error has already been handled in api.ts with init=true
371
+ // Just ensure we don't keep retrying with the bad token
372
+ flag.SQ = false;
373
+ }
374
+ }
375
+ catch (parseError) {
376
+ // Not a JSON error or not Error 141, continue with normal handling
377
+ }
364
378
  if ((0, inet_1.netConnectivityIssues)(error)) {
365
379
  flag.SQ = false;
366
380
  }
@@ -38,6 +38,9 @@ exports.MESSAGES = {
38
38
  REQUEST_TIMEOUT: (path) => `Request timeout for ${path || 'unknown'}`,
39
39
  REQUEST_ERROR: (path, message, code) => `Request error for ${path || 'unknown'}: ${message || 'Unknown error'} (${code || 'NO_CODE'})`,
40
40
  SOCKET_HANGUP_RETRY: (path, delay, attempt, max) => `Socket hang up detected. Retrying ${path || 'unknown'} with ${delay} ms delay (attempt ${attempt}/${max})`,
41
+ ERROR_141_DETECTED: 'Error 141: Invalid sync_token detected. Token is no longer valid.',
42
+ ERROR_141_RECOVERY: 'Attempting automatic recovery with init=true to get fresh token.',
43
+ ERROR_141_RETRY: 'Retrying sync operation with fresh initialization after Error 141.',
41
44
  },
42
45
  // Plugin messages (plugins.ts)
43
46
  PLUGINS: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@contentstack/datasync-manager",
3
3
  "author": "Contentstack LLC <support@contentstack.com>",
4
- "version": "2.3.0",
4
+ "version": "2.4.0-beta.0",
5
5
  "description": "The primary module of Contentstack DataSync. Syncs Contentstack data with your server using Contentstack Sync API",
6
6
  "main": "dist/index.js",
7
7
  "dependencies": {