error.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const MongoNetworkError = require('./core').MongoNetworkError;
  3. const MONGODB_ERROR_CODES = require('./error_codes').MONGODB_ERROR_CODES;
  4. // From spec@https://github.com/mongodb/specifications/blob/f93d78191f3db2898a59013a7ed5650352ef6da8/source/change-streams/change-streams.rst#resumable-error
  5. const GET_MORE_RESUMABLE_CODES = new Set([
  6. MONGODB_ERROR_CODES.HostUnreachable,
  7. MONGODB_ERROR_CODES.HostNotFound,
  8. MONGODB_ERROR_CODES.NetworkTimeout,
  9. MONGODB_ERROR_CODES.ShutdownInProgress,
  10. MONGODB_ERROR_CODES.PrimarySteppedDown,
  11. MONGODB_ERROR_CODES.ExceededTimeLimit,
  12. MONGODB_ERROR_CODES.SocketException,
  13. MONGODB_ERROR_CODES.NotMaster,
  14. MONGODB_ERROR_CODES.InterruptedAtShutdown,
  15. MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
  16. MONGODB_ERROR_CODES.NotMasterNoSlaveOk,
  17. MONGODB_ERROR_CODES.NotMasterOrSecondary,
  18. MONGODB_ERROR_CODES.StaleShardVersion,
  19. MONGODB_ERROR_CODES.StaleEpoch,
  20. MONGODB_ERROR_CODES.StaleConfig,
  21. MONGODB_ERROR_CODES.RetryChangeStream,
  22. MONGODB_ERROR_CODES.FailedToSatisfyReadPreference,
  23. MONGODB_ERROR_CODES.CursorNotFound
  24. ]);
  25. function isResumableError(error, wireVersion) {
  26. if (error instanceof MongoNetworkError) {
  27. return true;
  28. }
  29. if (wireVersion >= 9) {
  30. // DRIVERS-1308: For 4.4 drivers running against 4.4 servers, drivers will add a special case to treat the CursorNotFound error code as resumable
  31. if (error.code === MONGODB_ERROR_CODES.CursorNotFound) {
  32. return true;
  33. }
  34. return error.hasErrorLabel('ResumableChangeStreamError');
  35. }
  36. return GET_MORE_RESUMABLE_CODES.has(error.code);
  37. }
  38. module.exports = { GET_MORE_RESUMABLE_CODES, isResumableError, MONGODB_ERROR_CODES };