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 656e3cc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 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
7 changes: 4 additions & 3 deletions test/integration/node-specific/abstract_cursor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { inspect } from 'util';

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

describe('class AbstractCursor', function () {
describe.only('class AbstractCursor', function () {
describe('regression tests NODE-5372', function () {
let client: MongoClient;
let collection: Collection;
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('class AbstractCursor', function () {
});
});

describe('cursor iteration APIs', function () {
describe.only('cursor iteration APIs', function () {
let client: MongoClient;
let collection: Collection;
const transformSpy = sinon.spy(doc => ({ ...doc, name: doc.name.toUpperCase() }));
Expand Down 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 656e3cc

Please sign in to comment.