Skip to content

Commit

Permalink
Add errno to distinguish between request timeout and body download ti…
Browse files Browse the repository at this point in the history
…meout (#1184)

It is stretching the meaning of posix codes a bit, but at least avoids inventing new ones
  • Loading branch information
kornelski authored and focusaurus committed Feb 22, 2017
1 parent 52bd175 commit e383f5a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,14 @@ RequestBase.prototype.sortQuery = function(sort) {
* @api private
*/

RequestBase.prototype._timeoutError = function(reason, timeout){
RequestBase.prototype._timeoutError = function(reason, timeout, errno){
if (this._aborted) {
return;
}
var err = new Error(reason + timeout + 'ms exceeded');
err.timeout = timeout;
err.code = 'ECONNABORTED';
err.errno = errno;
this.timedout = true;
this.abort();
this.callback(err);
Expand All @@ -578,13 +579,13 @@ RequestBase.prototype._setTimeouts = function() {
// deadline
if (this._timeout && !this._timer) {
this._timer = setTimeout(function(){
self._timeoutError('Timeout of ', self._timeout);
self._timeoutError('Timeout of ', self._timeout, 'ETIME');
}, this._timeout);
}
// response timeout
if (this._responseTimeout && !this._responseTimeoutTimer) {
this._responseTimeoutTimer = setTimeout(function(){
self._timeoutError('Response timeout of ', self._responseTimeout);
self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
}, this._responseTimeout);
}
}
2 changes: 2 additions & 0 deletions test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe('.timeout(ms)', function(){
.end(function(err, res){
assert(err, 'expected an error');
assert.equal('ECONNABORTED', err.code, 'expected abort error code')
assert.equal('ETIME', err.errno);
done();
});
});
Expand All @@ -76,6 +77,7 @@ describe('.timeout(ms)', function(){
assert(err, 'expected an error');
assert.equal('number', typeof err.timeout, 'expected an error with .timeout');
assert.equal('ECONNABORTED', err.code, 'expected abort error code')
assert.equal('ETIMEDOUT', err.errno);
done();
});
});
Expand Down

0 comments on commit e383f5a

Please sign in to comment.