From 6339625a2e99355fb0068e334461ae1eb3bde533 Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Thu, 10 Aug 2017 10:28:18 -0400 Subject: [PATCH] fix(cursor): `hasNext` should propagate errors when using callback NODE-1119 --- lib/cursor.js | 3 ++- test/functional/cursor_tests.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/cursor.js b/lib/cursor.js index ffaf6f1d93..585a900222 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -221,7 +221,8 @@ Cursor.prototype.hasNext = function(callback) { return callback(null, true); } else { return nextObject(self, function(err, doc) { - if(!doc) return callback(null, false); + if (err) return callback(err, null); + if (!doc) return callback(null, false); self.s.currentDoc = doc; callback(null, true); }); diff --git a/test/functional/cursor_tests.js b/test/functional/cursor_tests.js index 4751bef87e..852ab909aa 100644 --- a/test/functional/cursor_tests.js +++ b/test/functional/cursor_tests.js @@ -3745,3 +3745,29 @@ exports['Correcly decorate the collection cursor count command with skip, limit, }); } } + +exports['Should propagate hasNext errors when using a callback'] = { + metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } }, + test: function(configuration, test) { + var client = configuration.newDbInstance({ w: 1 }, { poolSize: 1, auto_reconnect: false }); + client.connect(function(err, client) { + test.equal(null, err); + + var db = client.db(configuration.database); + var findCommand = { + find: 'integration_tests.has_next_error_callback', + limit: 0, + skip: 0, + query: {}, + slaveOk: false + }; + + var cursor = db.s.topology.cursor(db.s.namespace, findCommand, { readPreference: 42 }); + cursor.hasNext(function(err, hasNext) { + test.ok(err !== null); + test.equal(err.message, 'readPreference must be a ReadPreference instance'); + test.done(); + }); + }); + } +}