Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(commands): check doc.cursor errors
Browse files Browse the repository at this point in the history
Fixes NODE-1528
  • Loading branch information
kvwalker committed Jul 11, 2018
1 parent 2b82975 commit 4f2b263
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/connection/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var retrieveBSON = require('./utils').retrieveBSON;
var BSON = retrieveBSON();
var Long = BSON.Long;
const MongoError = require('../error').MongoError;

// Incrementing request id
var _requestId = 0;
Expand Down Expand Up @@ -484,6 +485,18 @@ Response.prototype.parse = function(options) {
// Deserialize but keep the array of documents in non-parsed form
var doc = this.bson.deserialize(document, _options);

if (doc instanceof Error) {
throw doc;
}

if (doc.errmsg) {
throw new MongoError(doc.errmsg);
}

if (!doc.cursor) {
throw new MongoError('Cursor not found');
}

// Get the documents
this.documents = doc.cursor[documentsReturnedIn];
this.numberReturned = this.documents.length;
Expand Down
66 changes: 66 additions & 0 deletions test/tests/unit/response_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
'use strict';

const expect = require('chai').expect;
const MongoError = require('../../../lib/error').MongoError;
const mock = require('mongodb-mock-server');
const Server = require('../../../lib/topologies/server');
const Long = require('bson').Long;

const test = {};
describe('Response', function() {
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(mockServer => {
test.server = mockServer;
});
});

it('should throw when document is error', {
metadata: { requires: { topology: ['single'] } },
test: function(done) {
const errdoc = {
errmsg: 'Cursor not found (namespace: "liveearth.entityEvents", id: 2018648316188432590).'
};

const client = new Server(test.server.address());

test.server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER, {
maxWireVersion: 6
})
);
} else if (doc.find) {
request.reply({
cursor: {
id: Long.fromNumber(1),
ns: 'test.test',
firstBatch: []
},
ok: 1
});
} else if (doc.getMore) {
request.reply(errdoc);
}
});

client.on('error', done);
client.once('connect', () => {
const cursor = client.cursor('test.test', { find: 'test' });

// Execute next
cursor.next(function(err) {
expect(err).to.exist;
expect(err).to.be.instanceof(MongoError);
expect(err.message).to.equal(errdoc.errmsg);

client.destroy();
done();
});
});
client.connect();
}
});
});

0 comments on commit 4f2b263

Please sign in to comment.