Skip to content

Commit

Permalink
http: add host and port info to ECONNRESET errors
Browse files Browse the repository at this point in the history
Add more information to the "ECONNRESET" errors generated when the
socket hang ups before receiving the response.

These kind of errors are really hard to troubleshoot without this info.
  • Loading branch information
jfromaniello committed Mar 30, 2017
1 parent 51d45db commit c995420
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
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();
}));
}));

0 comments on commit c995420

Please sign in to comment.