Skip to content

Commit

Permalink
fix(cursor): throw error in ChangeStream constructor if `changeStream…
Browse files Browse the repository at this point in the history
…Thunk()` throws a sync error
  • Loading branch information
vkarpov15 committed Aug 29, 2024
1 parent 69dacc2 commit 8667bdb
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/cursor/changeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,34 @@ class ChangeStream extends EventEmitter {
);
}

let syncError = null;
this.$driverChangeStreamPromise = new Promise((resolve, reject) => {
// This wrapper is necessary because of buffering.
changeStreamThunk((err, driverChangeStream) => {
if (err != null) {
this.emit('error', err);
return reject(err);
}
try {
changeStreamThunk((err, driverChangeStream) => {
if (err != null) {
this.emit('error', err);
return reject(err);
}

this.driverChangeStream = driverChangeStream;
this.emit('ready');
resolve();
});
this.driverChangeStream = driverChangeStream;
this.emit('ready');
resolve();
});
} catch (err) {
syncError = err;
this.emit('error', err);
reject(err);
}
});

// Because a ChangeStream is an event emitter, there's no way to register an 'error' handler
// that catches errors which occur in the constructor, unless we force sync errors into async
// errors with setImmediate(). For cleaner stack trace, we just immediately throw any synchronous
// errors that occurred with changeStreamThunk().
if (syncError != null) {
throw syncError;
}
}

_bindEvents() {
Expand Down

0 comments on commit 8667bdb

Please sign in to comment.