From 50cd07cd34ade1678a109c9523dd4d62be931706 Mon Sep 17 00:00:00 2001 From: duibu05 Date: Wed, 19 Jan 2022 00:40:18 +0800 Subject: [PATCH] Fixed The timeoutErrorMessage property in config not work with Node.js (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 * 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 * 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 Co-authored-by: Jay --- lib/adapters/http.js | 8 +++++++- test/unit/adapters/http.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index a3669de08e..c36f0c7f8d 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -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 diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index f17c9233ec..42c6358e72 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -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',