diff --git a/lib/core/Axios.js b/lib/core/Axios.js index 2713364a0a..2765bbbda3 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -46,12 +46,15 @@ class Axios { // slice off the Error: ... line const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : ''; - - if (!err.stack) { - err.stack = stack; - // match without the 2 top stack lines - } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) { - err.stack += '\n' + stack + try { + if (!err.stack) { + err.stack = stack; + // match without the 2 top stack lines + } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) { + err.stack += '\n' + stack + } + } catch (e) { + // ignore the case where "stack" is an un-writable property } } diff --git a/test/unit/core/Axios.js b/test/unit/core/Axios.js new file mode 100644 index 0000000000..8958ad1d0b --- /dev/null +++ b/test/unit/core/Axios.js @@ -0,0 +1,47 @@ +import Axios from "../../../lib/core/Axios.js"; +import assert from "assert"; + +describe('Axios', function () { + describe("handle un-writable error stack", function () { + async function testUnwritableErrorStack(stackAttributes) { + const axios = new Axios({}); + // mock axios._request to return an Error with an un-writable stack property + axios._request = () => { + const mockError = new Error("test-error"); + Object.defineProperty(mockError, "stack", stackAttributes); + throw mockError; + } + try { + await axios.request("test-url", {}) + } catch (e) { + assert.strictEqual(e.message, "test-error") + } + } + + it('should support errors with a defined but un-writable stack', async function () { + await testUnwritableErrorStack({value: {}, writable: false}) + }); + + it('should support errors with an undefined and un-writable stack', async function () { + await testUnwritableErrorStack({value: undefined, writable: false}) + }); + + it('should support errors with a custom getter/setter for the stack property', async function () { + await testUnwritableErrorStack({ + get: () => ({}), + set: () => { + throw new Error('read-only'); + } + }) + }); + + it('should support errors with a custom getter/setter for the stack property (null case)', async function () { + await testUnwritableErrorStack({ + get: () => null, + set: () => { + throw new Error('read-only'); + } + }) + }); + }) +});