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-4074): ensure getTopology doesn't throw synchronously #3172

Merged
merged 8 commits into from Mar 21, 2022

Conversation

baileympearson
Copy link
Contributor

@baileympearson baileympearson commented Mar 15, 2022

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.

  • All calls to getTopology that were used in conjunction with executeOperation were moved into executeOperation
  • Tests were added to ensure we're handling the errors appropriately with both callbacks and promises
  • Two other uses of getTopology (indexInformation and AddUserOperation.execute) were calling getTopology in an asynchronous context, so these two uses now handle the potential error thrown from getTopology.
  • getTopology was enhanced to take other topology providers that are used throughout the driver
Is there new documentation needed for these changes?

Nope!

Double check the following

  • Ran npm run check:lint script
  • Self-review completed using the steps outlined here
  • PR title follows the correct format: <type>(NODE-xxxx)<!>: <description>
  • Changes are covered by tests
  • New TODOs have a related JIRA ticket

src/utils.ts Outdated Show resolved Hide resolved
src/operations/execute_operation.ts Outdated Show resolved Hide resolved
src/utils.ts Show resolved Hide resolved
@nbbeeken nbbeeken added the Primary Review In Review with primary reviewer, not yet ready for team's eyes label Mar 16, 2022
@nbbeeken nbbeeken added Team Review Needs review from team and removed Primary Review In Review with primary reviewer, not yet ready for team's eyes labels Mar 16, 2022
@nbbeeken nbbeeken requested review from durran and dariakp March 16, 2022 19:48
Copy link
Contributor

@nbbeeken nbbeeken left a 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

@robertsLando
Copy link

Any news on this?

@nbbeeken nbbeeken merged commit 329f081 into main Mar 21, 2022
@nbbeeken nbbeeken deleted the NODE-4074-getTopology-should-not-throw branch March 21, 2022 21:23
@robertsLando
Copy link

robertsLando commented Apr 5, 2022

@baileympearson
Copy link
Contributor Author

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.

@robertsLando
Copy link

robertsLando commented Apr 5, 2022

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 next is called (like it was before). Wrapping every find with a try catch is much less user friendly

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
}

@baileympearson
Copy link
Contributor Author

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 catch block, not in the next function.

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();

@robertsLando
Copy link

robertsLando commented Apr 6, 2022

Just to clarify - you're saying that the behavior changed in v4.5.0

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Team Review Needs review from team
Projects
None yet
4 participants