Skip to content

Commit

Permalink
Added 'err' and 'timeout' events
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas committed Nov 22, 2016
1 parent 1223350 commit bacdb94
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/needle.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,17 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,

function had_error(err) {
debug('Request error', err);
out.emit('err', err);
done(err || new Error('Unknown error when making request.'));
}

function set_timeout(milisecs) {
function set_timeout(type, milisecs) {
if (milisecs <= 0) return;
timer = setTimeout(function() { request.abort() }, milisecs);

timer = setTimeout(function() {
out.emit('timeout', type);
request.abort();
}, milisecs);
}

// handle errors on the underlying socket, that may be closed while writing
Expand All @@ -415,7 +420,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,

// clear open timeout, and set a read timeout.
if (timer) clearTimeout(timer);
set_timeout(config.read_timeout);
set_timeout('read', config.read_timeout);

// if we got cookies, parse them unless we were instructed not to. make sure to include any
// cookies that might have been set on previous redirects.
Expand Down Expand Up @@ -617,7 +622,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
}); // end request call

// unless open_timeout was disabled, set a timeout to abort the request.
set_timeout(config.open_timeout);
set_timeout('open', config.open_timeout);

// handle errors on the request object. things might get bumpy.
request.on('error', had_error);
Expand Down

0 comments on commit bacdb94

Please sign in to comment.