Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/core/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
alexandre-abrioux marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
47 changes: 47 additions & 0 deletions test/unit/core/Axios.js
Original file line number Diff line number Diff line change
@@ -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');
}
})
});
})
});