@@ -599,13 +599,14 @@ function makeConnection(conn, options, callback) {
599
599
// Add handlers for events
600
600
connection . once ( 'error' , err => callback ( err , null ) ) ;
601
601
connection . once ( 'timeout' , err => callback ( err , null ) ) ;
602
- connection . once ( 'close' , err => callback ( err , null ) ) ;
602
+ // connection.once('close', err => callback(err, null));
603
603
return ;
604
604
}
605
605
606
606
function fastFallbackConnect ( conn , _options , callback ) {
607
607
const options = prepareConnectionOptions ( conn , _options ) ;
608
608
609
+ let errors = [ ] ;
609
610
let connection ;
610
611
const connectionHandler = ( err , _connection ) => {
611
612
if ( _connection ) {
@@ -618,16 +619,20 @@ function fastFallbackConnect(conn, _options, callback) {
618
619
connection = _connection ;
619
620
return callback ( null , connection ) ;
620
621
} else if ( err ) {
621
- if ( conn . logger . isDebug ( ) ) {
622
- conn . logger . debug (
623
- `connection ${ conn . id } for [${ conn . host } :${ conn . port } ] errored out with [${ JSON . stringify (
624
- err
625
- ) } ]`
626
- ) ;
622
+ if ( errors . length > 0 ) {
623
+ // an error occurred for the second time, we have officially failed
624
+
625
+ //empty errors array
626
+ errors = [ ] ;
627
+
628
+ // return mongo error to be emitted
629
+ return callback ( new MongoNetworkError ( 'failed to connect' ) , null ) ;
627
630
}
631
+
632
+ // otherwise push the error, and wait for subsequent connects
633
+ errors . push ( err ) ;
628
634
return ;
629
635
}
630
- return ;
631
636
} ;
632
637
633
638
makeConnection ( conn , Object . assign ( { } , { family : 6 } , options ) , connectionHandler ) ;
@@ -655,6 +660,7 @@ Connection.prototype.connect = function(_options) {
655
660
656
661
return fastFallbackConnect ( this , _options , ( err , connection ) => {
657
662
if ( err ) {
663
+ this . emit ( 'error' , err , this ) ;
658
664
return ;
659
665
}
660
666
// Add handlers for events
0 commit comments