Skip to content

Commit

Permalink
Merge branch 'v1.x' into feat/axios-fetch-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalBrainJS committed May 7, 2024
2 parents fd69f9f + 81e0455 commit f0c8da2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
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
}
}

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');
}
})
});
})
});

0 comments on commit f0c8da2

Please sign in to comment.