Skip to content

Commit

Permalink
Fixed The timeoutErrorMessage property in config not work with Node.js (
Browse files Browse the repository at this point in the history
fixes #3580) (#3581)

* The timeoutErrorMessage property in config not work with Node.js (#3580)

* Adding "should respect the timeoutErrorMessage property" test case

Co-authored-by: Will Loo <duibu05@126.com>

* The timeoutErrorMessage property in config not work with Node.js (#3580)

* Fixing The timeoutErrorMessage property in config not work with Node.js (#3580)

* Updating http adapter

* Adding reject config.timeoutErrorMessage when setup

Co-authored-by: Will Loo <duibu05@126.com>

* Fixing The timeoutErrorMessage property in config not work with Node.js (#3580)

* Fixing The timeoutErrorMessage property in config not work with Node.js (#3580)

* Update http adapter

* Make changes as suggested after code review

Co-authored-by: Will Loo <duibu05@126.com>

Co-authored-by: Jay <jasonsaayman@gmail.com>
  • Loading branch information
duibu05 and jasonsaayman committed Jan 18, 2022
1 parent 5c5cbdf commit 4461761
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/adapters/http.js
Expand Up @@ -349,9 +349,15 @@ module.exports = function httpAdapter(config) {
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
req.setTimeout(timeout, function handleRequestTimeout() {
req.abort();
var timeoutErrorMessage = '';
if (config.timeoutErrorMessage) {
timeoutErrorMessage = config.timeoutErrorMessage;
} else {
timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
}
var transitional = config.transitional || defaults.transitional;
reject(createError(
'timeout of ' + timeout + 'ms exceeded',
timeoutErrorMessage,
config,
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
req
Expand Down
30 changes: 30 additions & 0 deletions test/unit/adapters/http.js
Expand Up @@ -116,6 +116,36 @@ describe('supports http with nodejs', function () {
});
});

it('should respect the timeoutErrorMessage property', function (done) {

server = http.createServer(function (req, res) {
setTimeout(function () {
res.end();
}, 1000);
}).listen(4444, function () {
var success = false, failure = false;
var error;

axios.get('http://localhost:4444/', {
timeout: 250,
timeoutErrorMessage: 'oops, timeout',
}).then(function (res) {
success = true;
}).catch(function (err) {
error = err;
failure = true;
});

setTimeout(function () {
assert.strictEqual(success, false, 'request should not succeed');
assert.strictEqual(failure, true, 'request should fail');
assert.strictEqual(error.code, 'ECONNABORTED');
assert.strictEqual(error.message, 'oops, timeout');
done();
}, 300);
});
});

it('should allow passing JSON', function (done) {
var data = {
firstName: 'Fred',
Expand Down

0 comments on commit 4461761

Please sign in to comment.