Skip to content

Commit 24aead5

Browse files
committed
http: refactor emit error
1 parent ef57911 commit 24aead5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/_http_client.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ function ClientRequest(input, options, cb) {
265265
return;
266266
called = true;
267267
if (err) {
268-
process.nextTick(() => this.emit('error', err));
268+
process.nextTick(emitError, this, err);
269269
return;
270270
}
271271
this.onSocket(socket);
@@ -374,7 +374,7 @@ function socketCloseListener() {
374374
// receive a response. The error needs to
375375
// fire on the request.
376376
req.socket._hadError = true;
377-
req.emit('error', connResetException('socket hang up'));
377+
emitError(req, connResetException('socket hang up'));
378378
}
379379
req.emit('close');
380380
}
@@ -391,6 +391,12 @@ function socketCloseListener() {
391391
}
392392
}
393393

394+
function emitError(req, err) {
395+
if (!req.aborted) {
396+
req.emit('error', err);
397+
}
398+
}
399+
394400
function socketErrorListener(err) {
395401
const socket = this;
396402
const req = socket._httpMessage;
@@ -400,7 +406,7 @@ function socketErrorListener(err) {
400406
// For Safety. Some additional errors might fire later on
401407
// and we need to make sure we don't double-fire the error event.
402408
req.socket._hadError = true;
403-
req.emit('error', err);
409+
emitError(req, err);
404410
}
405411

406412
// Handle any pending data
@@ -434,7 +440,7 @@ function socketOnEnd() {
434440
// If we don't have a response then we know that the socket
435441
// ended prematurely and we need to emit an error on the request.
436442
req.socket._hadError = true;
437-
req.emit('error', connResetException('socket hang up'));
443+
emitError(req, connResetException('socket hang up'));
438444
}
439445
if (parser) {
440446
parser.finish();
@@ -457,7 +463,7 @@ function socketOnData(d) {
457463
freeParser(parser, req, socket);
458464
socket.destroy();
459465
req.socket._hadError = true;
460-
req.emit('error', ret);
466+
emitError(req, ret);
461467
} else if (parser.incoming && parser.incoming.upgrade) {
462468
// Upgrade (if status code 101) or CONNECT
463469
var bytesParsed = ret;

0 commit comments

Comments
 (0)