Skip to content

Commit 656e3cc

Browse files
close cursor on error from transform
1 parent 53e8cde commit 656e3cc

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { inspect } from 'util';
55

66
import { type Collection, type FindCursor, MongoAPIError, type MongoClient } from '../../mongodb';
77

8-
describe('class AbstractCursor', function () {
8+
describe.only('class AbstractCursor', function () {
99
describe('regression tests NODE-5372', function () {
1010
let client: MongoClient;
1111
let collection: Collection;
@@ -38,7 +38,7 @@ describe('class AbstractCursor', function () {
3838
});
3939
});
4040

41-
describe('cursor iteration APIs', function () {
41+
describe.only('cursor iteration APIs', function () {
4242
let client: MongoClient;
4343
let collection: Collection;
4444
const transformSpy = sinon.spy(doc => ({ ...doc, name: doc.name.toUpperCase() }));
@@ -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)