error.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const MongoNetworkError = require('mongodb-core').MongoNetworkError;
  3. const mongoErrorContextSymbol = require('mongodb-core').mongoErrorContextSymbol;
  4. const GET_MORE_NON_RESUMABLE_CODES = new Set([
  5. 136, // CappedPositionLost
  6. 237, // CursorKilled
  7. 11601 // Interrupted
  8. ]);
  9. // From spec@https://github.com/mongodb/specifications/blob/35e466ddf25059cb30e4113de71cdebd3754657f/source/change-streams.rst#resumable-error:
  10. //
  11. // An error is considered resumable if it meets any of the following criteria:
  12. // - any error encountered which is not a server error (e.g. a timeout error or network error)
  13. // - any server error response from a getMore command excluding those containing the following error codes
  14. // - Interrupted: 11601
  15. // - CappedPositionLost: 136
  16. // - CursorKilled: 237
  17. // - a server error response with an error message containing the substring "not master" or "node is recovering"
  18. //
  19. // An error on an aggregate command is not a resumable error. Only errors on a getMore command may be considered resumable errors.
  20. function isGetMoreError(error) {
  21. if (error[mongoErrorContextSymbol]) {
  22. return error[mongoErrorContextSymbol].isGetMore;
  23. }
  24. }
  25. function isResumableError(error) {
  26. if (!isGetMoreError(error)) {
  27. return false;
  28. }
  29. return !!(
  30. error instanceof MongoNetworkError ||
  31. !GET_MORE_NON_RESUMABLE_CODES.has(error.code) ||
  32. error.message.match(/not master/) ||
  33. error.message.match(/node is recovering/)
  34. );
  35. }
  36. module.exports = { GET_MORE_NON_RESUMABLE_CODES, isResumableError };