Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: add host and port info to ECONNRESET errors #7498

Closed
Closed
Show file tree
Hide file tree
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
14 changes: 11 additions & 3 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function ClientRequest(options, cb) {
options = util._extend({}, options);
}

self._requestOptions = options;

var agent = options.agent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;
if (agent === false) {
Expand Down Expand Up @@ -248,9 +250,15 @@ function emitAbortNT(self) {
}


function createHangUpError() {
function createHangUpError(req) {
var error = new Error('socket hang up');
error.code = 'ECONNRESET';
if (req._requestOptions.socketPath) {
error.path = req._requestOptions.socketPath;
} else {
error.host = req._requestOptions.host;
error.port = req._requestOptions.port;
}
return error;
}

Expand Down Expand Up @@ -281,7 +289,7 @@ function socketCloseListener() {
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.emit('error', createHangUpError());
req.emit('error', createHangUpError(req));
req.socket._hadError = true;
}

Expand Down Expand Up @@ -341,7 +349,7 @@ function socketOnEnd() {
if (!req.res && !req.socket._hadError) {
// 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.emit('error', createHangUpError());
req.emit('error', createHangUpError(req));
req.socket._hadError = true;
}
if (parser) {
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-http-econnreset-pipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

common.refreshTmpDir();

const server = net.createServer((c) => {
c.end();
});

server.listen(common.PIPE, common.mustCall(() => {
http.request({ socketPath: common.PIPE, path: '/', method: 'GET' })
.once('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ECONNRESET');
assert.strictEqual(err.port, undefined);
assert.strictEqual(err.host, undefined);
assert.strictEqual(err.path, common.PIPE);
server.close();
}));
}));
23 changes: 23 additions & 0 deletions test/parallel/test-http-econnreset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');
const net = require('net');

const server = net.createServer((c) => {
c.end();
});

server.listen(common.mustCall(() => {
const port = server.address().port;

http.get({ port: port, path: '/', host: common.localhostIPv4 })
.once('error', common.mustCall((err) => {
assert.strictEqual(err.code, 'ECONNRESET');
assert.strictEqual(err.port, port);
assert.strictEqual(err.host, common.localhostIPv4);
assert.strictEqual(err.path, undefined);
server.close();
}));
}));