From 34424ed67c1d8b295e6eae3c5911544c5e562a17 Mon Sep 17 00:00:00 2001 From: Bailey Pearson Date: Thu, 29 Jun 2023 17:08:30 -0600 Subject: [PATCH] close cursor on error from transform --- src/cursor/abstract_cursor.ts | 7 ++++++- test/integration/node-specific/abstract_cursor.test.ts | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cursor/abstract_cursor.ts b/src/cursor/abstract_cursor.ts index 2af1c98309..31a76ee384 100644 --- a/src/cursor/abstract_cursor.ts +++ b/src/cursor/abstract_cursor.ts @@ -705,7 +705,12 @@ async function next( 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; diff --git a/test/integration/node-specific/abstract_cursor.test.ts b/test/integration/node-specific/abstract_cursor.test.ts index 5a546b0a26..dc22cacc55 100644 --- a/test/integration/node-specific/abstract_cursor.test.ts +++ b/test/integration/node-specific/abstract_cursor.test.ts @@ -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'); @@ -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; }); } @@ -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); }); });