Skip to content

Commit 34424ed

Browse files
close cursor on error from transform
1 parent 53e8cde commit 34424ed

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/cursor/abstract_cursor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,12 @@ async function next<T>(
705705
const doc = cursor[kDocuments].shift();
706706

707707
if (doc != null && transform && cursor[kTransform]) {
708-
return cursor[kTransform](doc);
708+
try {
709+
return cursor[kTransform](doc);
710+
} catch (error) {
711+
await cleanupCursorAsync(cursor, { error, needsToEmitClosed: true });
712+
throw error;
713+
}
709714
}
710715

711716
return doc;

test/integration/node-specific/abstract_cursor.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ describe('class AbstractCursor', function () {
9696
expect(doc.name).to.equal('JOHN DOE');
9797
});
9898

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

@@ -130,6 +130,7 @@ describe('class AbstractCursor', function () {
130130
expect(error)
131131
.to.be.instanceOf(Error)
132132
.to.match(/error thrown in transform/);
133+
expect(cursor._cursor).to.have.property('closed', true);
133134
});
134135
});
135136

0 commit comments

Comments
 (0)