-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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-4074): ensure getTopology doesn't throw synchronously #3172
Conversation
test/integration/connection-monitoring-and-pooling/connection.test.js
Outdated
Show resolved
Hide resolved
test/integration/connection-monitoring-and-pooling/connection.test.js
Outdated
Show resolved
Hide resolved
test/integration/connection-monitoring-and-pooling/connection.test.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noticed 😅 We've got some lint errors to sort out
Any news on this? |
Seems that there are still some collections methods that could throw: https://github.com/mongodb/node-mongodb-native/blob/main/src/collection.ts#L762 https://github.com/mongodb/node-mongodb-native/blob/main/src/collection.ts#L1411 |
Hey @robertsLando! These methods can still throw. However, they're actually synchronous methods so throwing is the expected behavior (any method that returns a cursor is synchronous because cursors are lazy and don't fetch data until they're consumed). The other methods in this PR needed to be fixed because they took callbacks. Previously, if we didn't catch the error and the user passed a callback, we could crash the user's process because we weren't propagating the error up the callback chain. |
Yeah I know that (I'm the user who opened the issue and PR for this). I just saw the new release today and when I tested it I got this issue. While I understand what you mean I don't think this behaviour is correct as it's not working as before, in my opinion the error should be thrown when Before: collection.find({}).next( (err, doc) => {
// err contains the error connection is not opened
}) Now: try {
collection.find({}).next( (err, doc) => {
// never reached
})
} catch(error) {
// error contains the error connection is not opened
} |
Just to clarify - you're saying that the behavior changed in v4.5.0? I've tried in all versions 4.3.0 -> 4.5.0 and in all cases I see the error being caught in the import { MongoClient } from "mongodb";
async function run() {
const client = new MongoClient(< uri >);
const db = client.db('nodeTest');
const collection = db.collection('nodeTest');
try {
collection.find({}).next(( )=> {
console.log('caught error inside next');
});
} catch (err) {
console.log('error caught in catch')
}
}
run(); |
Nope this has changed in 4+ versions but I didn't see it reported as breaking change. Also as you can see in my example the code looks a bit less user friendly |
Description
This PR is a re-do of the work done by Daniel Lando (original PR: #3170). I messed up the rebase and accidentally removed all changes.
What is changing?
All uses of
getTopology
that are used in an asynchronous manner now catch any errors thrown and pass the error along to the appropriate callback.getTopology
that were used in conjunction withexecuteOperation
were moved intoexecuteOperation
getTopology
(indexInformation
andAddUserOperation.execute
) were calling getTopology in an asynchronous context, so these two uses now handle the potential error thrown fromgetTopology
.getTopology
was enhanced to take other topology providers that are used throughout the driverIs there new documentation needed for these changes?
Nope!
Double check the following
npm run check:lint
script<type>(NODE-xxxx)<!>: <description>