Skip to content

Commit 1a08f90

Browse files
fix: capture async stack only for rejections with native error objects; (#6203)
1 parent 104aa3f commit 1a08f90

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

lib/core/Axios.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ class Axios {
3939
try {
4040
return await this._request(configOrUrl, config);
4141
} catch (err) {
42-
const dummy = {}
43-
if (Error.captureStackTrace) {
44-
Error.captureStackTrace(dummy)
45-
} else {
46-
dummy.stack = new Error().stack;
47-
}
48-
// slice off the Error: ... line
49-
dummy.stack = dummy.stack.replace(/^.+\n/, '');
50-
// match without the 2 top stack lines
51-
if (!err.stack.endsWith(dummy.stack.replace(/^.+\n.+\n/, ''))) {
52-
err.stack += '\n' + dummy.stack
42+
if (err instanceof Error) {
43+
let dummy;
44+
45+
Error.captureStackTrace ? Error.captureStackTrace(dummy = {}) : (dummy = new Error());
46+
47+
// slice off the Error: ... line
48+
const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
49+
50+
if (!err.stack) {
51+
err.stack = stack;
52+
// match without the 2 top stack lines
53+
} else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
54+
err.stack += '\n' + stack
55+
}
5356
}
5457

5558
throw err;

test/unit/adapters/http.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -449,28 +449,29 @@ describe('supports http with nodejs', function () {
449449
});
450450
});
451451

452-
it('should wrap HTTP errors and keep stack', function (done) {
452+
it('should wrap HTTP errors and keep stack', async function () {
453453
if (nodeMajorVersion <= 12) {
454454
this.skip(); // node 12 support for async stack traces appears lacking
455455
return;
456456
}
457-
server = http.createServer(function (req, res) {
457+
458+
server = await startHTTPServer((req, res) => {
458459
res.statusCode = 400;
459460
res.end();
460-
}).listen(4444, function () {
461-
void assert.rejects(
462-
async function findMeInStackTrace() {
463-
await axios.head('http://localhost:4444/one')
464-
},
465-
function (err) {
466-
assert.equal(err.name, 'AxiosError')
467-
assert.equal(err.isAxiosError, true)
468-
const matches = [...err.stack.matchAll(/findMeInStackTrace/g)]
469-
assert.equal(matches.length, 1, err.stack)
470-
return true;
471-
}
472-
).then(done).catch(done);
473461
});
462+
463+
return assert.rejects(
464+
async function findMeInStackTrace() {
465+
await axios.head('http://localhost:4444/one')
466+
},
467+
function (err) {
468+
assert.equal(err.name, 'AxiosError')
469+
assert.equal(err.isAxiosError, true)
470+
const matches = [...err.stack.matchAll(/findMeInStackTrace/g)]
471+
assert.equal(matches.length, 1, err.stack)
472+
return true;
473+
}
474+
)
474475
});
475476

476477
it('should wrap interceptor errors and keep stack', function (done) {

0 commit comments

Comments
 (0)