Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function ClientRequest(input, options, cb) {
return;
called = true;
if (err) {
process.nextTick(() => this.emit('error', err));
process.nextTick(emitError, this, err);
return;
}
this.onSocket(socket);
Expand Down Expand Up @@ -374,7 +374,7 @@ function socketCloseListener() {
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
emitError(req, connResetException('socket hang up'));
}
req.emit('close');
}
Expand All @@ -391,6 +391,12 @@ function socketCloseListener() {
}
}

function emitError(req, err) {
if (!req.aborted) {
req.emit('error', err);
}
}

function socketErrorListener(err) {
const socket = this;
const req = socket._httpMessage;
Expand All @@ -400,7 +406,7 @@ function socketErrorListener(err) {
// For Safety. Some additional errors might fire later on
// and we need to make sure we don't double-fire the error event.
req.socket._hadError = true;
req.emit('error', err);
emitError(req, err);
}

// Handle any pending data
Expand Down Expand Up @@ -434,7 +440,7 @@ function socketOnEnd() {
// If we don't have a response then we know that the socket
// ended prematurely and we need to emit an error on the request.
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
emitError(req, connResetException('socket hang up'));
}
if (parser) {
parser.finish();
Expand All @@ -457,7 +463,7 @@ function socketOnData(d) {
freeParser(parser, req, socket);
socket.destroy();
req.socket._hadError = true;
req.emit('error', ret);
emitError(req, ret);
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade (if status code 101) or CONNECT
var bytesParsed = ret;
Expand Down