From 153646c6a4564a3d5c173b563ed0083a157b7490 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 2 Apr 2020 07:59:05 -0400 Subject: [PATCH] fix: don't throw if `withTransaction()` callback rejects with a null reason A logical error prevented receipt of errors returned from the lambda passed into the `withTransaction` helper. NODE-2515 --- lib/core/sessions.js | 1 + test/functional/transactions.test.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/core/sessions.js b/lib/core/sessions.js index fcd3384645..762f0541bf 100644 --- a/lib/core/sessions.js +++ b/lib/core/sessions.js @@ -304,6 +304,7 @@ function isUnknownTransactionCommitResult(err) { } function isMaxTimeMSExpiredError(err) { + if (err == null) return false; return ( err.code === MAX_TIME_MS_EXPIRED_CODE || (err.writeConcernError && err.writeConcernError.code === MAX_TIME_MS_EXPIRED_CODE) diff --git a/test/functional/transactions.test.js b/test/functional/transactions.test.js index f71e3f641e..95b2b2adca 100644 --- a/test/functional/transactions.test.js +++ b/test/functional/transactions.test.js @@ -75,6 +75,23 @@ describe('Transactions', function() { session.endSession(done); } }); + + it('should return readable error if promise rejected with no reason', { + metadata: { requires: { topology: ['replicaset', 'sharded'], mongodb: '>=4.0.2' } }, + test: function(done) { + function fnThatReturnsBadPromise() { + return Promise.reject(); + } + + session + .withTransaction(fnThatReturnsBadPromise) + .then(() => done(Error('Expected error'))) + .catch(err => { + expect(err).to.equal(undefined); + session.endSession(done); + }); + } + }); }); describe('startTransaction', function() {