Skip to content

Commit

Permalink
fix: add default error handler to avoid Unhandled error event throw
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
fengmk2 committed Aug 3, 2015
1 parent a020b0d commit 24afa4a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ Agent.prototype.createSocket = function(req, options) {
// set the default timer
s.setTimeout(self.timeout);

// Add a default error handler to avoid Unhandled 'error' event throw
// https://github.com/node-modules/agentkeepalive/issues/25
function onError(err) {
debug('CLIENT socket onError: %s', err);
self.emit('error', err);
}
s.on('error', onError);

function onRemove() {
// We need this function for cases like HTTP 'upgrade'
// (defined by WebSockets) where we need to remove a socket from the
Expand All @@ -266,6 +274,7 @@ Agent.prototype.createSocket = function(req, options) {
s.removeListener('close', onClose);
s.removeListener('free', onFree);
s.removeListener('agentRemove', onRemove);
s.removeListener('error', onError);
// remove timer
s.setTimeout(0, onTimeout);
}
Expand Down
7 changes: 7 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* * https://github.com/joyent/node/blob/master/lib/_http_agent.js
*
* Copyright(c) 2012 - 2014 fengmk2 <fengmk2@gmail.com>
* Copyright(c) node-modules
* MIT Licensed
*/

Expand Down Expand Up @@ -57,6 +58,8 @@ function Agent(options) {
var self = this;
self.createSocketCount = 0;
self.closeSocketCount = 0;
// socket error event count
self.errorSocketCount = 0;
self.requestCount = 0;
self.timeoutSocketCount = 0;
self.on('free', function () {
Expand All @@ -68,6 +71,9 @@ function Agent(options) {
self.on('close', function () {
self.closeSocketCount++;
});
self.on('error', function () {
self.errorSocketCount++;
});
}

util.inherits(Agent, OriginalAgent);
Expand All @@ -87,6 +93,7 @@ Agent.prototype.getCurrentStatus = function () {
return {
createSocketCount: this.createSocketCount,
closeSocketCount: this.closeSocketCount,
errorSocketCount: this.errorSocketCount,
timeoutSocketCount: this.timeoutSocketCount,
requestCount: this.requestCount,
freeSockets: inspect(this.freeSockets),
Expand Down
11 changes: 11 additions & 0 deletions test/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* agentkeepalive - test/agent.test.js
*
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
* Copyright(c) node-modules
* MIT Licensed
*/

Expand Down Expand Up @@ -799,4 +800,14 @@ describe('agent.test.js', function () {
});
});
});

describe('getCurrentStatus()', function () {
it('should get current agent status', function () {
var status = agentkeepalive.getCurrentStatus();
console.log(status);
status.should.have.keys('createSocketCount', 'closeSocketCount', 'errorSocketCount', 'timeoutSocketCount',
'requestCount', 'freeSockets', 'sockets', 'requests');
});
});

});
1 change: 1 addition & 0 deletions test/https_agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* agentkeepalive - test/https_agent.test.js
*
* Copyright(c) 2012 - 2013 fengmk2 <fengmk2@gmail.com>
* Copyright(c) node-modules
* MIT Licensed
*/

Expand Down

0 comments on commit 24afa4a

Please sign in to comment.