Skip to content

Commit

Permalink
fix(cursor): hasNext should propagate errors when using callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mbroadst committed Aug 10, 2017
1 parent 75f1135 commit 6339625
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
26 changes: 26 additions & 0 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
}
}

0 comments on commit 6339625

Please sign in to comment.