@@ -386,15 +386,35 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
386386 if ( existingSocket ) {
387387 resolve ( socket ) ;
388388 } else {
389+ const start = performance . now ( ) ;
389390 const connectEvent = useTLS ? 'secureConnect' : 'connect' ;
390391 socket
391392 . once ( connectEvent , ( ) => resolve ( socket ) )
392- . once ( 'error' , error => reject ( connectionFailureError ( 'error' , error ) ) )
393- . once ( 'timeout' , ( ) => reject ( connectionFailureError ( 'timeout' ) ) )
394- . once ( 'close' , ( ) => reject ( connectionFailureError ( 'close' ) ) ) ;
393+ . once ( 'error' , cause =>
394+ reject ( new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) )
395+ )
396+ . once ( 'timeout' , ( ) => {
397+ reject (
398+ new MongoNetworkTimeoutError (
399+ `Socket '${ connectEvent } ' timed out after ${ ( performance . now ( ) - start ) | 0 } ms (connectTimeoutMS: ${ connectTimeoutMS } )`
400+ )
401+ ) ;
402+ } )
403+ . once ( 'close' , ( ) =>
404+ reject (
405+ new MongoNetworkError (
406+ `Socket closed after ${ ( performance . now ( ) - start ) | 0 } during connection establishment`
407+ )
408+ )
409+ ) ;
395410
396411 if ( options . cancellationToken != null ) {
397- cancellationHandler = ( ) => reject ( connectionFailureError ( 'cancel' ) ) ;
412+ cancellationHandler = ( ) =>
413+ reject (
414+ new MongoNetworkError (
415+ `Socket connection establishment was cancelled after ${ ( performance . now ( ) - start ) | 0 } `
416+ )
417+ ) ;
398418 options . cancellationToken . once ( 'cancel' , cancellationHandler ) ;
399419 }
400420 }
@@ -447,9 +467,11 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
447467
448468 socks ??= loadSocks ( ) ;
449469
470+ let existingSocket : Stream ;
471+
450472 try {
451473 // Then, establish the Socks5 proxy connection:
452- const { socket } = await socks . SocksClient . createConnection ( {
474+ const connection = await socks . SocksClient . createConnection ( {
453475 existing_socket : rawSocket ,
454476 timeout : options . connectTimeoutMS ,
455477 command : 'connect' ,
@@ -466,35 +488,12 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
466488 password : options . proxyPassword || undefined
467489 }
468490 } ) ;
469-
470- // Finally, now treat the resulting duplex stream as the
471- // socket over which we send and receive wire protocol messages:
472- return await makeSocket ( {
473- ...options ,
474- existingSocket : socket ,
475- proxyHost : undefined
476- } ) ;
477- } catch ( error ) {
478- throw connectionFailureError ( 'error' , error ) ;
491+ existingSocket = connection . socket ;
492+ } catch ( cause ) {
493+ throw new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) ;
479494 }
480- }
481495
482- function connectionFailureError ( type : 'error' , cause : Error ) : MongoNetworkError ;
483- function connectionFailureError ( type : 'close' | 'timeout' | 'cancel' ) : MongoNetworkError ;
484- function connectionFailureError (
485- type : 'error' | 'close' | 'timeout' | 'cancel' ,
486- cause ?: Error
487- ) : MongoNetworkError {
488- switch ( type ) {
489- case 'error' :
490- return new MongoNetworkError ( MongoError . buildErrorMessage ( cause ) , { cause } ) ;
491- case 'timeout' :
492- return new MongoNetworkTimeoutError ( 'connection timed out' ) ;
493- case 'close' :
494- return new MongoNetworkError ( 'connection closed' ) ;
495- case 'cancel' :
496- return new MongoNetworkError ( 'connection establishment was cancelled' ) ;
497- default :
498- return new MongoNetworkError ( 'unknown network error' ) ;
499- }
496+ // Finally, now treat the resulting duplex stream as the
497+ // socket over which we send and receive wire protocol messages:
498+ return await makeSocket ( { ...options , existingSocket, proxyHost : undefined } ) ;
500499}
0 commit comments