This repository was archived by the owner on Feb 4, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ const MongoNetworkError = require('./error').MongoNetworkError;
1111const MongoWriteConcernError = require ( './error' ) . MongoWriteConcernError ;
1212const Transaction = require ( './transactions' ) . Transaction ;
1313const TxnState = require ( './transactions' ) . TxnState ;
14+ const isPromiseLike = require ( './utils' ) . isPromiseLike ;
1415
1516function assertAlive ( session , callback ) {
1617 if ( session . serverSession == null ) {
@@ -328,7 +329,19 @@ function userExplicitlyEndedTransaction(session) {
328329function attemptTransaction ( session , startTime , fn , options ) {
329330 session . startTransaction ( options ) ;
330331
331- return fn ( session )
332+ let promise ;
333+ try {
334+ promise = fn ( session ) ;
335+ } catch ( err ) {
336+ promise = Promise . reject ( err ) ;
337+ }
338+
339+ if ( ! isPromiseLike ( promise ) ) {
340+ session . abortTransaction ( ) ;
341+ throw new TypeError ( 'Function provided to `withTransaction` must return a Promise' ) ;
342+ }
343+
344+ return promise
332345 . then ( ( ) => {
333346 if ( userExplicitlyEndedTransaction ( session ) ) {
334347 return ;
Original file line number Diff line number Diff line change @@ -106,12 +106,23 @@ function collationNotSupported(server, cmd) {
106106 return cmd && cmd . collation && maxWireVersion ( server ) < 5 ;
107107}
108108
109+ /**
110+ * Checks if a given value is a Promise
111+ *
112+ * @param {* } maybePromise
113+ * @return true if the provided value is a Promise
114+ */
115+ function isPromiseLike ( maybePromise ) {
116+ return maybePromise && typeof maybePromise . then === 'function' ;
117+ }
118+
109119module . exports = {
110120 uuidV4,
111121 calculateDurationInMs,
112122 relayEvents,
113123 collationNotSupported,
114124 retrieveEJSON,
115125 retrieveKerberos,
116- maxWireVersion
126+ maxWireVersion,
127+ isPromiseLike
117128} ;
You can’t perform that action at this time.
0 commit comments