@@ -90,8 +90,19 @@ const kClientRequestStatistics = Symbol('ClientRequestStatistics');
9090
9191const  dc  =  require ( 'diagnostics_channel' ) ; 
9292const  onClientRequestStartChannel  =  dc . channel ( 'http.client.request.start' ) ; 
93+ const  onClientRequestErrorChannel  =  dc . channel ( 'http.client.request.error' ) ; 
9394const  onClientResponseFinishChannel  =  dc . channel ( 'http.client.response.finish' ) ; 
9495
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+ 
95106const  {  addAbortSignal,  finished }  =  require ( 'stream' ) ; 
96107
97108let  debug  =  require ( 'internal/util/debuglog' ) . debuglog ( 'http' ,  ( fn )  =>  { 
@@ -343,7 +354,7 @@ function ClientRequest(input, options, cb) {
343354    if  ( typeof  opts . createConnection  ===  'function' )  { 
344355      const  oncreate  =  once ( ( err ,  socket )  =>  { 
345356        if  ( err )  { 
346-           process . nextTick ( ( )  =>  this . emit ( 'error' ,  err ) ) ; 
357+           process . nextTick ( ( )  =>  emitErrorEvent ( this ,  err ) ) ; 
347358        }  else  { 
348359          this . onSocket ( socket ) ; 
349360        } 
@@ -465,7 +476,7 @@ function socketCloseListener() {
465476      // receive a response. The error needs to 
466477      // fire on the request. 
467478      req . socket . _hadError  =  true ; 
468-       req . emit ( 'error' ,  new  ConnResetException ( 'socket hang up' ) ) ; 
479+       emitErrorEvent ( req ,  new  ConnResetException ( 'socket hang up' ) ) ; 
469480    } 
470481    req . _closed  =  true ; 
471482    req . emit ( 'close' ) ; 
@@ -492,7 +503,7 @@ function socketErrorListener(err) {
492503    // For Safety. Some additional errors might fire later on 
493504    // and we need to make sure we don't double-fire the error event. 
494505    req . socket . _hadError  =  true ; 
495-     req . emit ( 'error' ,  err ) ; 
506+     emitErrorEvent ( req ,  err ) ; 
496507  } 
497508
498509  const  parser  =  socket . parser ; 
@@ -516,7 +527,7 @@ function socketOnEnd() {
516527    // If we don't have a response then we know that the socket 
517528    // ended prematurely and we need to emit an error on the request. 
518529    req . socket . _hadError  =  true ; 
519-     req . emit ( 'error' ,  new  ConnResetException ( 'socket hang up' ) ) ; 
530+     emitErrorEvent ( req ,  new  ConnResetException ( 'socket hang up' ) ) ; 
520531  } 
521532  if  ( parser )  { 
522533    parser . finish ( ) ; 
@@ -541,7 +552,7 @@ function socketOnData(d) {
541552    socket . removeListener ( 'end' ,  socketOnEnd ) ; 
542553    socket . destroy ( ) ; 
543554    req . socket . _hadError  =  true ; 
544-     req . emit ( 'error' ,  ret ) ; 
555+     emitErrorEvent ( req ,  ret ) ; 
545556  }  else  if  ( parser . incoming  &&  parser . incoming . upgrade )  { 
546557    // Upgrade (if status code 101) or CONNECT 
547558    const  bytesParsed  =  ret ; 
@@ -872,7 +883,7 @@ function onSocketNT(req, socket, err) {
872883        err  =  new  ConnResetException ( 'socket hang up' ) ; 
873884      } 
874885      if  ( err )  { 
875-         req . emit ( 'error' ,  err ) ; 
886+         emitErrorEvent ( req ,  err ) ; 
876887      } 
877888      req . _closed  =  true ; 
878889      req . emit ( 'close' ) ; 
0 commit comments