Skip to content

Commit

Permalink
fix: throw timeout error in promise interface when timedout (#1510)
Browse files Browse the repository at this point in the history
* fix: return timeout error in promise interface

* test: timeout in promise interface
  • Loading branch information
a631807682 authored and niftylettuce committed Jan 7, 2020
1 parent 6cb8d95 commit 56ce517
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ RequestBase.prototype._retry = function() {

this._aborted = false;
this.timedout = false;
this.timedoutError = null;

return this._end();
};
Expand All @@ -246,6 +247,11 @@ RequestBase.prototype.then = function(resolve, reject) {

this._fullfilledPromise = new Promise((resolve, reject) => {
self.on('abort', () => {
if (this.timedout && this.timedoutError) {
reject(this.timedoutError);
return;
}

const err = new Error('Aborted');
err.code = 'ABORTED';
err.status = this.status;
Expand Down Expand Up @@ -714,6 +720,7 @@ RequestBase.prototype._timeoutError = function(reason, timeout, errno) {
err.code = 'ECONNABORTED';
err.errno = errno;
this.timedout = true;
this.timedoutError = err;
this.abort();
this.callback(err);
};
Expand Down
16 changes: 16 additions & 0 deletions test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ describe('.timeout(ms)', function() {
});
});

it('should error in promise interface ', done => {
request
.get(`${base}/delay/500`)
.timeout(150)
.catch(err => {
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');
done();
});
});

it('should handle gzip timeout', done => {
request
.get(`${base}/delay/zip`)
Expand Down

0 comments on commit 56ce517

Please sign in to comment.