@@ -419,14 +419,14 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
419
419
* Commits the currently active transaction in this session.
420
420
*/
421
421
async commitTransaction ( ) : Promise < void > {
422
- return await endTransactionAsync ( this , 'commitTransaction' ) ;
422
+ return await endTransaction ( this , 'commitTransaction' ) ;
423
423
}
424
424
425
425
/**
426
426
* Aborts the currently active transaction in this session.
427
427
*/
428
428
async abortTransaction ( ) : Promise < void > {
429
- return await endTransactionAsync ( this , 'abortTransaction' ) ;
429
+ return await endTransaction ( this , 'abortTransaction' ) ;
430
430
}
431
431
432
432
/**
@@ -637,25 +637,15 @@ async function attemptTransaction<T>(
637
637
}
638
638
}
639
639
640
- const endTransactionAsync = promisify (
641
- endTransaction as (
642
- session : ClientSession ,
643
- commandName : 'abortTransaction' | 'commitTransaction' ,
644
- callback : ( error : Error ) => void
645
- ) => void
646
- ) ;
647
-
648
- function endTransaction (
640
+ async function endTransaction (
649
641
session : ClientSession ,
650
- commandName : 'abortTransaction' | 'commitTransaction' ,
651
- callback : Callback < void >
652
- ) {
642
+ commandName : 'abortTransaction' | 'commitTransaction'
643
+ ) : Promise < void > {
653
644
// handle any initial problematic cases
654
645
const txnState = session . transaction . state ;
655
646
656
647
if ( txnState === TxnState . NO_TRANSACTION ) {
657
- callback ( new MongoTransactionError ( 'No transaction started' ) ) ;
658
- return ;
648
+ throw new MongoTransactionError ( 'No transaction started' ) ;
659
649
}
660
650
661
651
if ( commandName === 'commitTransaction' ) {
@@ -665,37 +655,28 @@ function endTransaction(
665
655
) {
666
656
// the transaction was never started, we can safely exit here
667
657
session . transaction . transition ( TxnState . TRANSACTION_COMMITTED_EMPTY ) ;
668
- callback ( ) ;
669
658
return ;
670
659
}
671
660
672
661
if ( txnState === TxnState . TRANSACTION_ABORTED ) {
673
- callback (
674
- new MongoTransactionError ( 'Cannot call commitTransaction after calling abortTransaction' )
675
- ) ;
676
- return ;
662
+ throw new MongoTransactionError ( 'Cannot call commitTransaction after calling abortTransaction' ) ;
677
663
}
678
664
} else {
679
665
if ( txnState === TxnState . STARTING_TRANSACTION ) {
680
666
// the transaction was never started, we can safely exit here
681
667
session . transaction . transition ( TxnState . TRANSACTION_ABORTED ) ;
682
- callback ( ) ;
683
668
return ;
684
669
}
685
670
686
671
if ( txnState === TxnState . TRANSACTION_ABORTED ) {
687
- callback ( new MongoTransactionError ( 'Cannot call abortTransaction twice' ) ) ;
688
- return ;
672
+ throw new MongoTransactionError ( 'Cannot call abortTransaction twice' ) ;
689
673
}
690
674
691
675
if (
692
676
txnState === TxnState . TRANSACTION_COMMITTED ||
693
677
txnState === TxnState . TRANSACTION_COMMITTED_EMPTY
694
678
) {
695
- callback (
696
- new MongoTransactionError ( 'Cannot call abortTransaction after calling commitTransaction' )
697
- ) ;
698
- return ;
679
+ throw new MongoTransactionError ( 'Cannot call abortTransaction after calling commitTransaction' ) ;
699
680
}
700
681
}
701
682
@@ -728,9 +709,8 @@ function endTransaction(
728
709
if ( session . loadBalanced ) {
729
710
maybeClearPinnedConnection ( session , { force : false } ) ;
730
711
}
731
-
732
- // The spec indicates that we should ignore all errors on `abortTransaction`
733
- return callback ( ) ;
712
+ // The spec indicates that if the operation times out or fails with a non-retryable error, we should ignore all errors on `abortTransaction`
713
+ return ;
734
714
}
735
715
736
716
session . transaction . transition ( TxnState . TRANSACTION_COMMITTED ) ;
@@ -751,14 +731,14 @@ function endTransaction(
751
731
}
752
732
}
753
733
754
- callback ( error ) ;
734
+ throw error ;
755
735
}
756
736
757
737
if ( session . transaction . recoveryToken ) {
758
738
command . recoveryToken = session . transaction . recoveryToken ;
759
739
}
760
740
761
- const handleFirstCommandAttempt = ( error ?: Error ) => {
741
+ const handleFirstCommandAttempt = async ( error ?: Error ) => {
762
742
if ( command . abortTransaction ) {
763
743
// always unpin on abort regardless of command outcome
764
744
session . unpin ( ) ;
@@ -775,29 +755,33 @@ function endTransaction(
775
755
} ) ;
776
756
}
777
757
778
- executeOperation (
758
+ await executeOperation (
779
759
session . client ,
780
760
new RunAdminCommandOperation ( command , {
781
761
session,
782
762
readPreference : ReadPreference . primary ,
783
763
bypassPinningCheck : true
784
764
} )
785
- ) . then ( ( ) => commandHandler ( ) , commandHandler ) ;
786
- return ;
765
+ ) . catch ( e => commandHandler ( e ) ) ;
766
+ commandHandler ( ) ;
787
767
}
788
-
789
768
commandHandler ( error ) ;
790
769
} ;
791
770
792
- // send the command
793
- executeOperation (
794
- session . client ,
795
- new RunAdminCommandOperation ( command , {
796
- session,
797
- readPreference : ReadPreference . primary ,
798
- bypassPinningCheck : true
799
- } )
800
- ) . then ( ( ) => handleFirstCommandAttempt ( ) , handleFirstCommandAttempt ) ;
771
+ try {
772
+ // send the command
773
+ await executeOperation (
774
+ session . client ,
775
+ new RunAdminCommandOperation ( command , {
776
+ session,
777
+ readPreference : ReadPreference . primary ,
778
+ bypassPinningCheck : true
779
+ } )
780
+ ) ;
781
+ await handleFirstCommandAttempt ( ) ;
782
+ } catch ( e ) {
783
+ await handleFirstCommandAttempt ( e ) ;
784
+ }
801
785
}
802
786
803
787
/** @public */
0 commit comments