Skip to content

Commit 49fbafd

Browse files
committed
feat(changeStream): expanding changeStream resumable errors
Fixes NODE-1462
1 parent fa1a3c5 commit 49fbafd

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

lib/change_stream.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,50 @@ ChangeStream.prototype.stream = function(options) {
296296
return this.cursor.stream(options);
297297
};
298298

299+
const RESUMABLE_ERROR_CODES = new Set([
300+
6, // HostUnreachable
301+
7, // HostNotFound
302+
50, // ExceededTimeLimit
303+
89, // NetworkTimeout
304+
189, // PrimarySteppedDown
305+
216, // ElectionInProgress
306+
234, // RetryChangeStream
307+
9001, // SocketException
308+
10107, // NotMaster
309+
11602, // InterruptedDueToReplStateChange
310+
13435, // NotMasterNoSlaveOk
311+
13436 // NotMasterOrSecondary
312+
]);
313+
314+
// TODO: will be used for check for getMore errors
315+
// const GET_MORE_NON_RESUMABLE_CODES = new Set([
316+
// 136, // CappedPositionLost
317+
// 237, // CursorKilled
318+
// 11601 // Interrupted
319+
// ]);
320+
321+
function isResumableError(error) {
322+
// TODO: Need a way to check if error is
323+
// - from a getMore
324+
// - is not in GET_MORE_NON_RESUMABLE_CODES
325+
if (
326+
error instanceof MongoNetworkError ||
327+
RESUMABLE_ERROR_CODES.has(error.code) ||
328+
error.message.match(/not master/) ||
329+
error.message.match(/node is recovering/)
330+
) {
331+
return true;
332+
}
333+
334+
return false;
335+
}
336+
299337
// Handle new change events. This method brings together the routes from the callback, event emitter, and promise ways of using ChangeStream.
300338
var processNewChange = function(self, err, change, callback) {
301339
// Handle errors
302340
if (err) {
303341
// Handle resumable MongoNetworkErrors
304-
if (err instanceof MongoNetworkError && !self.attemptingResume) {
342+
if (isResumableError(err) && !self.attemptingResume) {
305343
self.attemptingResume = true;
306344
return self.cursor.close(function(closeErr) {
307345
if (closeErr) {

0 commit comments

Comments
 (0)