@@ -90,8 +90,19 @@ const kClientRequestStatistics = Symbol('ClientRequestStatistics');
90
90
91
91
const dc = require ( 'diagnostics_channel' ) ;
92
92
const onClientRequestStartChannel = dc . channel ( 'http.client.request.start' ) ;
93
+ const onClientRequestErrorChannel = dc . channel ( 'http.client.request.error' ) ;
93
94
const onClientResponseFinishChannel = dc . channel ( 'http.client.response.finish' ) ;
94
95
96
+ function emitErrorEvent ( request , error ) {
97
+ if ( onClientRequestErrorChannel . hasSubscribers ) {
98
+ onClientRequestErrorChannel . publish ( {
99
+ request,
100
+ error,
101
+ } ) ;
102
+ }
103
+ request . emit ( 'error' , error ) ;
104
+ }
105
+
95
106
const { addAbortSignal, finished } = require ( 'stream' ) ;
96
107
97
108
let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'http' , ( fn ) => {
@@ -343,7 +354,7 @@ function ClientRequest(input, options, cb) {
343
354
if ( typeof opts . createConnection === 'function' ) {
344
355
const oncreate = once ( ( err , socket ) => {
345
356
if ( err ) {
346
- process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
357
+ process . nextTick ( ( ) => emitErrorEvent ( this , err ) ) ;
347
358
} else {
348
359
this . onSocket ( socket ) ;
349
360
}
@@ -465,7 +476,7 @@ function socketCloseListener() {
465
476
// receive a response. The error needs to
466
477
// fire on the request.
467
478
req . socket . _hadError = true ;
468
- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
479
+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
469
480
}
470
481
req . _closed = true ;
471
482
req . emit ( 'close' ) ;
@@ -492,7 +503,7 @@ function socketErrorListener(err) {
492
503
// For Safety. Some additional errors might fire later on
493
504
// and we need to make sure we don't double-fire the error event.
494
505
req . socket . _hadError = true ;
495
- req . emit ( 'error' , err ) ;
506
+ emitErrorEvent ( req , err ) ;
496
507
}
497
508
498
509
const parser = socket . parser ;
@@ -516,7 +527,7 @@ function socketOnEnd() {
516
527
// If we don't have a response then we know that the socket
517
528
// ended prematurely and we need to emit an error on the request.
518
529
req . socket . _hadError = true ;
519
- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
530
+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
520
531
}
521
532
if ( parser ) {
522
533
parser . finish ( ) ;
@@ -541,7 +552,7 @@ function socketOnData(d) {
541
552
socket . removeListener ( 'end' , socketOnEnd ) ;
542
553
socket . destroy ( ) ;
543
554
req . socket . _hadError = true ;
544
- req . emit ( 'error' , ret ) ;
555
+ emitErrorEvent ( req , ret ) ;
545
556
} else if ( parser . incoming && parser . incoming . upgrade ) {
546
557
// Upgrade (if status code 101) or CONNECT
547
558
const bytesParsed = ret ;
@@ -872,7 +883,7 @@ function onSocketNT(req, socket, err) {
872
883
err = new ConnResetException ( 'socket hang up' ) ;
873
884
}
874
885
if ( err ) {
875
- req . emit ( 'error' , err ) ;
886
+ emitErrorEvent ( req , err ) ;
876
887
}
877
888
req . _closed = true ;
878
889
req . emit ( 'close' ) ;
0 commit comments