Skip to content

Commit

Permalink
fix: don't throw if withTransaction() callback rejects with a null …
Browse files Browse the repository at this point in the history
…reason

A logical error prevented receipt of errors returned from the lambda passed
into the `withTransaction` helper.

NODE-2515
  • Loading branch information
vkarpov15 committed Apr 2, 2020
1 parent 7778977 commit 153646c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/core/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions test/functional/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 153646c

Please sign in to comment.