Skip to content

Commit e5975af

Browse files
authored
fix(NODE-3463): pass explain error through to callback (#2949)
1 parent 238a4b0 commit e5975af

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/core/wireprotocol/query.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ function query(server, ns, cmd, cursorState, options, callback) {
3737

3838
// If we have explain, we need to rewrite the find command
3939
// to wrap it in the explain command
40-
const explain = Explain.fromOptions(options);
41-
if (explain) {
42-
findCmd = decorateWithExplain(findCmd, explain);
40+
try {
41+
const explain = Explain.fromOptions(options);
42+
if (explain) {
43+
findCmd = decorateWithExplain(findCmd, explain);
44+
}
45+
} catch (err) {
46+
return callback(err);
4347
}
4448

4549
// NOTE: This actually modifies the passed in cmd, and our code _depends_ on this

test/functional/explain.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const chai = require('chai');
33
const expect = chai.expect;
44
const withClient = require('./shared').withClient;
55
const setupDatabase = require('./shared').setupDatabase;
6+
const MongoError = require('../../index').MongoError;
67

78
describe('Explain', function() {
89
before(function() {
@@ -762,4 +763,43 @@ describe('Explain', function() {
762763
});
763764
})
764765
);
766+
767+
it('should throw a catchable error with invalid explain string (promise)', {
768+
metadata: {
769+
requires: {
770+
mongodb: '>=3.4'
771+
}
772+
},
773+
test: withClient(function(client, done) {
774+
const db = client.db('shouldThrowCatchableError');
775+
const collection = db.collection('test');
776+
collection
777+
.find({ a: 1 })
778+
.explain('invalidExplain')
779+
.then(() => done(new Error('expected explain to fail but it succeeded')))
780+
.catch(err => {
781+
expect(err).to.exist;
782+
expect(err).to.be.instanceOf(MongoError);
783+
done();
784+
});
785+
})
786+
});
787+
788+
it('should throw a catchable error with invalid explain string (callback)', {
789+
metadata: {
790+
requires: {
791+
mongodb: '>=3.4'
792+
}
793+
},
794+
test: withClient(function(client, done) {
795+
const db = client.db('shouldThrowCatchableError');
796+
const collection = db.collection('test');
797+
collection.find({ a: 1 }).explain('invalidExplain', (err, result) => {
798+
expect(err).to.exist;
799+
expect(result).to.not.exist;
800+
expect(err).to.be.instanceOf(MongoError);
801+
done();
802+
});
803+
})
804+
});
765805
});

0 commit comments

Comments
 (0)