@@ -386,15 +386,35 @@ export async function makeSocket(options: MakeConnectionOptions): Promise<Stream
386
386
if ( existingSocket ) {
387
387
resolve ( socket ) ;
388
388
} else {
389
+ const start = performance . now ( ) ;
389
390
const connectEvent = useTLS ? 'secureConnect' : 'connect' ;
390
391
socket
391
392
. 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
+ ) ;
395
410
396
411
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
+ ) ;
398
418
options . cancellationToken . once ( 'cancel' , cancellationHandler ) ;
399
419
}
400
420
}
@@ -447,9 +467,11 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
447
467
448
468
socks ??= loadSocks ( ) ;
449
469
470
+ let existingSocket : Stream ;
471
+
450
472
try {
451
473
// Then, establish the Socks5 proxy connection:
452
- const { socket } = await socks . SocksClient . createConnection ( {
474
+ const connection = await socks . SocksClient . createConnection ( {
453
475
existing_socket : rawSocket ,
454
476
timeout : options . connectTimeoutMS ,
455
477
command : 'connect' ,
@@ -466,35 +488,12 @@ async function makeSocks5Connection(options: MakeConnectionOptions): Promise<Str
466
488
password : options . proxyPassword || undefined
467
489
}
468
490
} ) ;
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 } ) ;
479
494
}
480
- }
481
495
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 } ) ;
500
499
}
0 commit comments