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

Commit 4f2b263

Browse files
authored
fix(commands): check doc.cursor errors
Fixes NODE-1528
1 parent 2b82975 commit 4f2b263

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

lib/connection/commands.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var retrieveBSON = require('./utils').retrieveBSON;
44
var BSON = retrieveBSON();
55
var Long = BSON.Long;
6+
const MongoError = require('../error').MongoError;
67

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

488+
if (doc instanceof Error) {
489+
throw doc;
490+
}
491+
492+
if (doc.errmsg) {
493+
throw new MongoError(doc.errmsg);
494+
}
495+
496+
if (!doc.cursor) {
497+
throw new MongoError('Cursor not found');
498+
}
499+
487500
// Get the documents
488501
this.documents = doc.cursor[documentsReturnedIn];
489502
this.numberReturned = this.documents.length;

test/tests/unit/response_test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const MongoError = require('../../../lib/error').MongoError;
5+
const mock = require('mongodb-mock-server');
6+
const Server = require('../../../lib/topologies/server');
7+
const Long = require('bson').Long;
8+
9+
const test = {};
10+
describe('Response', function() {
11+
afterEach(() => mock.cleanup());
12+
beforeEach(() => {
13+
return mock.createServer().then(mockServer => {
14+
test.server = mockServer;
15+
});
16+
});
17+
18+
it('should throw when document is error', {
19+
metadata: { requires: { topology: ['single'] } },
20+
test: function(done) {
21+
const errdoc = {
22+
errmsg: 'Cursor not found (namespace: "liveearth.entityEvents", id: 2018648316188432590).'
23+
};
24+
25+
const client = new Server(test.server.address());
26+
27+
test.server.setMessageHandler(request => {
28+
const doc = request.document;
29+
if (doc.ismaster) {
30+
request.reply(
31+
Object.assign({}, mock.DEFAULT_ISMASTER, {
32+
maxWireVersion: 6
33+
})
34+
);
35+
} else if (doc.find) {
36+
request.reply({
37+
cursor: {
38+
id: Long.fromNumber(1),
39+
ns: 'test.test',
40+
firstBatch: []
41+
},
42+
ok: 1
43+
});
44+
} else if (doc.getMore) {
45+
request.reply(errdoc);
46+
}
47+
});
48+
49+
client.on('error', done);
50+
client.once('connect', () => {
51+
const cursor = client.cursor('test.test', { find: 'test' });
52+
53+
// Execute next
54+
cursor.next(function(err) {
55+
expect(err).to.exist;
56+
expect(err).to.be.instanceof(MongoError);
57+
expect(err.message).to.equal(errdoc.errmsg);
58+
59+
client.destroy();
60+
done();
61+
});
62+
});
63+
client.connect();
64+
}
65+
});
66+
});

0 commit comments

Comments
 (0)