@@ -95,8 +95,19 @@ const kClientRequestStatistics = Symbol('ClientRequestStatistics');
95
95
96
96
const dc = require ( 'diagnostics_channel' ) ;
97
97
const onClientRequestStartChannel = dc . channel ( 'http.client.request.start' ) ;
98
+ const onClientRequestErrorChannel = dc . channel ( 'http.client.request.error' ) ;
98
99
const onClientResponseFinishChannel = dc . channel ( 'http.client.response.finish' ) ;
99
100
101
+ function emitErrorEvent ( request , error ) {
102
+ if ( onClientRequestErrorChannel . hasSubscribers ) {
103
+ onClientRequestErrorChannel . publish ( {
104
+ request,
105
+ error,
106
+ } ) ;
107
+ }
108
+ request . emit ( 'error' , error ) ;
109
+ }
110
+
100
111
const { addAbortSignal, finished } = require ( 'stream' ) ;
101
112
102
113
let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'http' , ( fn ) => {
@@ -348,7 +359,7 @@ function ClientRequest(input, options, cb) {
348
359
if ( typeof opts . createConnection === 'function' ) {
349
360
const oncreate = once ( ( err , socket ) => {
350
361
if ( err ) {
351
- process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
362
+ process . nextTick ( ( ) => emitErrorEvent ( this , err ) ) ;
352
363
} else {
353
364
this . onSocket ( socket ) ;
354
365
}
@@ -470,7 +481,7 @@ function socketCloseListener() {
470
481
// receive a response. The error needs to
471
482
// fire on the request.
472
483
req . socket . _hadError = true ;
473
- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
484
+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
474
485
}
475
486
req . _closed = true ;
476
487
req . emit ( 'close' ) ;
@@ -497,7 +508,7 @@ function socketErrorListener(err) {
497
508
// For Safety. Some additional errors might fire later on
498
509
// and we need to make sure we don't double-fire the error event.
499
510
req . socket . _hadError = true ;
500
- req . emit ( 'error' , err ) ;
511
+ emitErrorEvent ( req , err ) ;
501
512
}
502
513
503
514
const parser = socket . parser ;
@@ -521,7 +532,7 @@ function socketOnEnd() {
521
532
// If we don't have a response then we know that the socket
522
533
// ended prematurely and we need to emit an error on the request.
523
534
req . socket . _hadError = true ;
524
- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
535
+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
525
536
}
526
537
if ( parser ) {
527
538
parser . finish ( ) ;
@@ -546,7 +557,7 @@ function socketOnData(d) {
546
557
socket . removeListener ( 'end' , socketOnEnd ) ;
547
558
socket . destroy ( ) ;
548
559
req . socket . _hadError = true ;
549
- req . emit ( 'error' , ret ) ;
560
+ emitErrorEvent ( req , ret ) ;
550
561
} else if ( parser . incoming && parser . incoming . upgrade ) {
551
562
// Upgrade (if status code 101) or CONNECT
552
563
const bytesParsed = ret ;
@@ -877,7 +888,7 @@ function onSocketNT(req, socket, err) {
877
888
err = new ConnResetException ( 'socket hang up' ) ;
878
889
}
879
890
if ( err ) {
880
- req . emit ( 'error' , err ) ;
891
+ emitErrorEvent ( req , err ) ;
881
892
}
882
893
req . _closed = true ;
883
894
req . emit ( 'close' ) ;
0 commit comments