From a2f56b0df14b5b6cdac224c5c556328d4977bf7f Mon Sep 17 00:00:00 2001 From: Will Loo Date: Sat, 23 Jan 2021 01:17:48 +0800 Subject: [PATCH 1/3] The timeoutErrorMessage property in config not work with Node.js (#3580) * Adding "should respect the timeoutErrorMessage property" test case Co-authored-by: Will Loo --- test/unit/adapters/http.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 497eb28f73..6211e42277 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -57,6 +57,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', From 5df1126ba28087382db92ad0e16c3dc036eea05c Mon Sep 17 00:00:00 2001 From: Will Loo Date: Sat, 23 Jan 2021 01:23:32 +0800 Subject: [PATCH 2/3] 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 --- lib/adapters/http.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index f32241f77e..9125e50531 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -277,7 +277,11 @@ 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(config.timeout, function handleRequestTimeout() { req.abort(); - reject(createError('timeout of ' + config.timeout + 'ms exceeded', config, 'ECONNABORTED', req)); + var timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded'; + if (config.timeoutErrorMessage) { + timeoutErrorMessage = config.timeoutErrorMessage; + } + reject(createError(timeoutErrorMessage, config, 'ECONNABORTED', req)); }); } From a381d5cddd6ddc44aa42ab8ac29e48be30887114 Mon Sep 17 00:00:00 2001 From: Will Loo Date: Tue, 17 Aug 2021 19:18:37 +0800 Subject: [PATCH 3/3] 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 --- lib/adapters/http.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 87b389c351..7541983975 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -300,9 +300,11 @@ 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 = 'timeout of ' + config.timeout + 'ms exceeded'; + var timeoutErrorMessage = ''; if (config.timeoutErrorMessage) { timeoutErrorMessage = config.timeoutErrorMessage; + } else { + timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded'; } reject(createError(timeoutErrorMessage, config, 'ECONNABORTED', req)); });