Skip to content

Commit 153646c

Browse files
authored
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
1 parent 7778977 commit 153646c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/core/sessions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ function isUnknownTransactionCommitResult(err) {
304304
}
305305

306306
function isMaxTimeMSExpiredError(err) {
307+
if (err == null) return false;
307308
return (
308309
err.code === MAX_TIME_MS_EXPIRED_CODE ||
309310
(err.writeConcernError && err.writeConcernError.code === MAX_TIME_MS_EXPIRED_CODE)

test/functional/transactions.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ describe('Transactions', function() {
7575
session.endSession(done);
7676
}
7777
});
78+
79+
it('should return readable error if promise rejected with no reason', {
80+
metadata: { requires: { topology: ['replicaset', 'sharded'], mongodb: '>=4.0.2' } },
81+
test: function(done) {
82+
function fnThatReturnsBadPromise() {
83+
return Promise.reject();
84+
}
85+
86+
session
87+
.withTransaction(fnThatReturnsBadPromise)
88+
.then(() => done(Error('Expected error')))
89+
.catch(err => {
90+
expect(err).to.equal(undefined);
91+
session.endSession(done);
92+
});
93+
}
94+
});
7895
});
7996

8097
describe('startTransaction', function() {

0 commit comments

Comments
 (0)