@@ -265,7 +265,7 @@ function ClientRequest(input, options, cb) {
265
265
return ;
266
266
called = true ;
267
267
if ( err ) {
268
- process . nextTick ( ( ) => this . emit ( 'error' , err ) ) ;
268
+ process . nextTick ( emitError , this , err ) ;
269
269
return ;
270
270
}
271
271
this . onSocket ( socket ) ;
@@ -374,7 +374,7 @@ function socketCloseListener() {
374
374
// receive a response. The error needs to
375
375
// fire on the request.
376
376
req . socket . _hadError = true ;
377
- req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
377
+ emitError ( req , connResetException ( 'socket hang up' ) ) ;
378
378
}
379
379
req . emit ( 'close' ) ;
380
380
}
@@ -391,6 +391,12 @@ function socketCloseListener() {
391
391
}
392
392
}
393
393
394
+ function emitError ( req , err ) {
395
+ if ( ! req . aborted ) {
396
+ req . emit ( 'error' , err ) ;
397
+ }
398
+ }
399
+
394
400
function socketErrorListener ( err ) {
395
401
const socket = this ;
396
402
const req = socket . _httpMessage ;
@@ -400,7 +406,7 @@ function socketErrorListener(err) {
400
406
// For Safety. Some additional errors might fire later on
401
407
// and we need to make sure we don't double-fire the error event.
402
408
req . socket . _hadError = true ;
403
- req . emit ( 'error' , err ) ;
409
+ emitError ( req , err ) ;
404
410
}
405
411
406
412
// Handle any pending data
@@ -434,7 +440,7 @@ function socketOnEnd() {
434
440
// If we don't have a response then we know that the socket
435
441
// ended prematurely and we need to emit an error on the request.
436
442
req . socket . _hadError = true ;
437
- req . emit ( 'error' , connResetException ( 'socket hang up' ) ) ;
443
+ emitError ( req , connResetException ( 'socket hang up' ) ) ;
438
444
}
439
445
if ( parser ) {
440
446
parser . finish ( ) ;
@@ -457,7 +463,7 @@ function socketOnData(d) {
457
463
freeParser ( parser , req , socket ) ;
458
464
socket . destroy ( ) ;
459
465
req . socket . _hadError = true ;
460
- req . emit ( 'error' , ret ) ;
466
+ emitError ( req , ret ) ;
461
467
} else if ( parser . incoming && parser . incoming . upgrade ) {
462
468
// Upgrade (if status code 101) or CONNECT
463
469
var bytesParsed = ret ;
0 commit comments