2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

fix(core/axios): handle un-writable error stack (#6362)

Co-authored-by: Dmitriy Mozgovoy <robotshara@gmail.com>
This commit is contained in:
Alexandre ABRIOUX
2024-05-07 19:57:03 +02:00
committed by GitHub
parent d1d359da34
commit 81e0455b7b
2 changed files with 56 additions and 6 deletions
+9 -6
View File
@@ -46,12 +46,15 @@ class Axios {
// slice off the Error: ... line // slice off the Error: ... line
const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : ''; const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
try {
if (!err.stack) { if (!err.stack) {
err.stack = stack; err.stack = stack;
// match without the 2 top stack lines // match without the 2 top stack lines
} else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) { } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
err.stack += '\n' + stack err.stack += '\n' + stack
}
} catch (e) {
// ignore the case where "stack" is an un-writable property
} }
} }
+47
View File
@@ -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');
}
})
});
})
});