Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-3463): pass explain error through to callback #2949

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/core/wireprotocol/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ function query(server, ns, cmd, cursorState, options, callback) {

// If we have explain, we need to rewrite the find command
// to wrap it in the explain command
const explain = Explain.fromOptions(options);
if (explain) {
findCmd = decorateWithExplain(findCmd, explain);
try {
const explain = Explain.fromOptions(options);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NODE-3377
Totally expanding scope here but we're validating too strictly here, this could be a quick fix that goes in this PR, we just need to assert string or boolean.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not do that in this PR because while a "quick fix", it also should come with its own test, and probably merits its own line in bug fix release notes. I don't object to getting it into 3.7 as a follow up PR, but let's keep small things small.

if (explain) {
findCmd = decorateWithExplain(findCmd, explain);
}
} catch (err) {
return callback(err);
}

// NOTE: This actually modifies the passed in cmd, and our code _depends_ on this
Expand Down
40 changes: 40 additions & 0 deletions test/functional/explain.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const chai = require('chai');
const expect = chai.expect;
const withClient = require('./shared').withClient;
const setupDatabase = require('./shared').setupDatabase;
const MongoError = require('../../index').MongoError;

describe('Explain', function() {
before(function() {
Expand Down Expand Up @@ -762,4 +763,43 @@ describe('Explain', function() {
});
})
);

it('should throw a catchable error with invalid explain string (promise)', {
metadata: {
requires: {
mongodb: '>=3.4'
}
},
test: withClient(function(client, done) {
const db = client.db('shouldThrowCatchableError');
const collection = db.collection('test');
collection
.find({ a: 1 })
.explain('invalidExplain')
.then(() => done(new Error('expected explain to fail but it succeeded')))
.catch(err => {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
expect(err).to.exist;
expect(err).to.be.instanceOf(MongoError);
done();
});
})
});

it('should throw a catchable error with invalid explain string (callback)', {
metadata: {
requires: {
mongodb: '>=3.4'
}
},
test: withClient(function(client, done) {
const db = client.db('shouldThrowCatchableError');
const collection = db.collection('test');
collection.find({ a: 1 }).explain('invalidExplain', (err, result) => {
expect(err).to.exist;
expect(result).to.not.exist;
expect(err).to.be.instanceOf(MongoError);
done();
});
})
});
});