Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-2035

Exceptions thrown from awaited cursor.forEach iterator do not propagate

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.10
    • Affects Version/s: 3.2.6
    • Component/s: Core
    • Not Needed

      When using the promised cursor.forEach (that is, without a callback), any exception thrown from within the iterator does not propagate to the outer awaiting continuation. Further, the exception is treated as unhandled by the node runtime resulting in the application crashing.

       

      Consider this simplified example:

      try {
          await cursor.forEach(doc => {
              throw new Error('BLAMMO!');    
          });
      }
      catch(ex) {   
      }
       

       

      I believe the default expectation of users of this library is that this exception would be thrown from the await continuation.

      Unfortunately, the actual result is `uncaught exception: Error: BLAMMO!`

      I'm thinking that this invocation https://github.com/mongodb/node-mongodb-native/blob/master/lib/cursor.js#L770  should be wrapped in a try/catch with a reject() call in the catch firewall.

      It would be my pleasure to PR this change, if I could get preliminary approval of the approach from the project maintainers.

       

            Assignee:
            warren.james@mongodb.com Warren James
            Reporter:
            brandonlwhite Brandon White
            Neal Beeken
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: