error.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. const MongoNetworkError = require('./core').MongoNetworkError;
  3. const mongoErrorContextSymbol = require('./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/7a2e93d85935ee4b1046a8d2ad3514c657dc74fa/source/change-streams/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 error label
  14. // NonRetryableChangeStreamError and those containing the following error codes:
  15. // - Interrupted: 11601
  16. // - CappedPositionLost: 136
  17. // - CursorKilled: 237
  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. if (error instanceof MongoNetworkError) {
  30. return true;
  31. }
  32. return !(
  33. GET_MORE_NON_RESUMABLE_CODES.has(error.code) ||
  34. error.hasErrorLabel('NonRetryableChangeStreamError')
  35. );
  36. }
  37. module.exports = { GET_MORE_NON_RESUMABLE_CODES, isResumableError };