Skip to content

Commit

Permalink
close cursor on error from transform
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Jun 29, 2023
1 parent 53e8cde commit 34424ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,12 @@ async function next<T>(
const doc = cursor[kDocuments].shift();

if (doc != null && transform && cursor[kTransform]) {
return cursor[kTransform](doc);
try {
return cursor[kTransform](doc);
} catch (error) {
await cleanupCursorAsync(cursor, { error, needsToEmitClosed: true });
throw error;
}
}

return doc;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/node-specific/abstract_cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe('class AbstractCursor', function () {
expect(doc.name).to.equal('JOHN DOE');
});

// skipped because these tests fail after throwing uncaught exceptions
it(`when the transform throws, ${method}() propagates the error to the user`, async () => {
const cursor = collection.find().map(() => {
throw new Error('error thrown in transform');
Expand All @@ -106,6 +105,7 @@ describe('class AbstractCursor', function () {
expect(error)
.to.be.instanceOf(Error)
.to.match(/error thrown in transform/);
expect(cursor.closed).to.be.true;
});
}

Expand All @@ -130,6 +130,7 @@ describe('class AbstractCursor', function () {
expect(error)
.to.be.instanceOf(Error)
.to.match(/error thrown in transform/);
expect(cursor._cursor).to.have.property('closed', true);
});
});

Expand Down

0 comments on commit 34424ed

Please sign in to comment.