@@ -95,8 +95,19 @@ const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9595
9696const dc = require ( 'diagnostics_channel' ) ;
9797const onClientRequestStartChannel = dc . channel ( 'http.client.request.start' ) ;
98+ const onClientRequestErrorChannel = dc . channel ( 'http.client.request.error' ) ;
9899const onClientResponseFinishChannel = dc . channel ( 'http.client.response.finish' ) ;
99100
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+
100111const { addAbortSignal, finished } = require ( 'stream' ) ;
101112
102113let debug = require ( 'internal/util/debuglog' ) . debuglog ( 'http' , ( fn ) => {
@@ -348,7 +359,7 @@ function ClientRequest(input, options, cb) {
348359 if ( typeof opts . createConnection === 'function' ) {
349360 const oncreate = once ( ( err , socket ) => {
350361 if ( err ) {
351- process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
362+ process . nextTick ( ( ) => emitErrorEvent ( this , err ) ) ;
352363 } else {
353364 this . onSocket ( socket ) ;
354365 }
@@ -470,7 +481,7 @@ function socketCloseListener() {
470481 // receive a response. The error needs to
471482 // fire on the request.
472483 req . socket . _hadError = true ;
473- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
484+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
474485 }
475486 req . _closed = true ;
476487 req . emit ( 'close' ) ;
@@ -497,7 +508,7 @@ function socketErrorListener(err) {
497508 // For Safety. Some additional errors might fire later on
498509 // and we need to make sure we don't double-fire the error event.
499510 req . socket . _hadError = true ;
500- req . emit ( 'error' , err ) ;
511+ emitErrorEvent ( req , err ) ;
501512 }
502513
503514 const parser = socket . parser ;
@@ -521,7 +532,7 @@ function socketOnEnd() {
521532 // If we don't have a response then we know that the socket
522533 // ended prematurely and we need to emit an error on the request.
523534 req . socket . _hadError = true ;
524- req . emit ( 'error' , new ConnResetException ( 'socket hang up' ) ) ;
535+ emitErrorEvent ( req , new ConnResetException ( 'socket hang up' ) ) ;
525536 }
526537 if ( parser ) {
527538 parser . finish ( ) ;
@@ -546,7 +557,7 @@ function socketOnData(d) {
546557 socket . removeListener ( 'end' , socketOnEnd ) ;
547558 socket . destroy ( ) ;
548559 req . socket . _hadError = true ;
549- req . emit ( 'error' , ret ) ;
560+ emitErrorEvent ( req , ret ) ;
550561 } else if ( parser . incoming && parser . incoming . upgrade ) {
551562 // Upgrade (if status code 101) or CONNECT
552563 const bytesParsed = ret ;
@@ -877,7 +888,7 @@ function onSocketNT(req, socket, err) {
877888 err = new ConnResetException ( 'socket hang up' ) ;
878889 }
879890 if ( err ) {
880- req . emit ( 'error' , err ) ;
891+ emitErrorEvent ( req , err ) ;
881892 }
882893 req . _closed = true ;
883894 req . emit ( 'close' ) ;
0 commit comments