From 3723a9757f27a527bfca22cc4c4736ad4423379c Mon Sep 17 00:00:00 2001 From: Devin Pitcher Date: Tue, 19 May 2020 16:17:47 -0700 Subject: [PATCH] Set request object on axios errors --- src/utils.js | 3 +++ test/abort_request.spec.js | 1 + test/basics.spec.js | 13 +++++++++++++ test/network_error.spec.js | 1 + test/timeout.spec.js | 2 ++ 5 files changed, 20 insertions(+) diff --git a/src/utils.js b/src/utils.js index 6e73734..f2d9405 100644 --- a/src/utils.js +++ b/src/utils.js @@ -148,6 +148,9 @@ function createAxiosError(message, config, response, code) { var error = new Error(message); error.isAxiosError = true; error.config = config; + error.request = { + responseUrl: config.url, + }; if (response !== undefined) { error.response = response; } diff --git a/test/abort_request.spec.js b/test/abort_request.spec.js index 219e05f..2a9faf6 100644 --- a/test/abort_request.spec.js +++ b/test/abort_request.spec.js @@ -28,6 +28,7 @@ describe("requestAborted spec", function () { expect(error.code).to.equal("ECONNABORTED"); expect(error.message).to.equal("Request aborted"); expect(error.isAxiosError).to.be.true; + expect(error.request.responseUrl).to.equal("/foo"); } ); }); diff --git a/test/basics.spec.js b/test/basics.spec.js index d5d9539..9975975 100644 --- a/test/basics.spec.js +++ b/test/basics.spec.js @@ -847,4 +847,17 @@ describe("MockAdapter basics", function () { expect(error.isAxiosError).to.be.true; }); }); + + it("sets the original request url in the response.request.responseUrl property on errors", function () { + mock.onGet("/foo").reply(404); + + return instance + .get("/foo") + .then(function () { + expect(true).to.be.false; + }) + .catch(function (error) { + expect(error.request.responseUrl).to.equal("/foo"); + }); + }); }); diff --git a/test/network_error.spec.js b/test/network_error.spec.js index 493fbc0..092cc44 100644 --- a/test/network_error.spec.js +++ b/test/network_error.spec.js @@ -24,6 +24,7 @@ describe("networkError spec", function () { expect(error.response).to.not.exist; expect(error.message).to.equal("Network Error"); expect(error.isAxiosError).to.be.true; + expect(error.request.responseUrl).to.equal("/foo"); } ); }); diff --git a/test/timeout.spec.js b/test/timeout.spec.js index 4745deb..c2256d3 100644 --- a/test/timeout.spec.js +++ b/test/timeout.spec.js @@ -24,6 +24,7 @@ describe("timeout spec", function () { expect(error.code).to.equal("ECONNABORTED"); expect(error.message).to.equal("timeout of 0ms exceeded"); expect(error.isAxiosError).to.be.true; + expect(error.request.responseUrl).to.equal("/foo"); } ); }); @@ -61,6 +62,7 @@ describe("timeout spec", function () { expect(error.code).to.equal("ECONNABORTED"); expect(error.message).to.equal(timeoutErrorMessage); expect(error.isAxiosError).to.be.true; + expect(error.request.responseUrl).to.equal("/foo"); } ); });