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

fix: capture async stack only for rejections with native error objects; (#6203)

This commit is contained in:
Dmitriy Mozgovoy
2024-01-25 20:42:38 +02:00
committed by GitHub
parent 104aa3f65d
commit 1a08f90f40
2 changed files with 30 additions and 26 deletions
+12 -9
View File
@@ -39,17 +39,20 @@ class Axios {
try { try {
return await this._request(configOrUrl, config); return await this._request(configOrUrl, config);
} catch (err) { } catch (err) {
const dummy = {} if (err instanceof Error) {
if (Error.captureStackTrace) { let dummy;
Error.captureStackTrace(dummy)
} else { Error.captureStackTrace ? Error.captureStackTrace(dummy = {}) : (dummy = new Error());
dummy.stack = new Error().stack;
}
// slice off the Error: ... line // slice off the Error: ... line
dummy.stack = dummy.stack.replace(/^.+\n/, ''); const stack = dummy.stack ? dummy.stack.replace(/^.+\n/, '') : '';
if (!err.stack) {
err.stack = stack;
// match without the 2 top stack lines // match without the 2 top stack lines
if (!err.stack.endsWith(dummy.stack.replace(/^.+\n.+\n/, ''))) { } else if (stack && !String(err.stack).endsWith(stack.replace(/^.+\n.+\n/, ''))) {
err.stack += '\n' + dummy.stack err.stack += '\n' + stack
}
} }
throw err; throw err;
+7 -6
View File
@@ -449,16 +449,18 @@ describe('supports http with nodejs', function () {
}); });
}); });
it('should wrap HTTP errors and keep stack', function (done) { it('should wrap HTTP errors and keep stack', async function () {
if (nodeMajorVersion <= 12) { if (nodeMajorVersion <= 12) {
this.skip(); // node 12 support for async stack traces appears lacking this.skip(); // node 12 support for async stack traces appears lacking
return; return;
} }
server = http.createServer(function (req, res) {
server = await startHTTPServer((req, res) => {
res.statusCode = 400; res.statusCode = 400;
res.end(); res.end();
}).listen(4444, function () { });
void assert.rejects(
return assert.rejects(
async function findMeInStackTrace() { async function findMeInStackTrace() {
await axios.head('http://localhost:4444/one') await axios.head('http://localhost:4444/one')
}, },
@@ -469,8 +471,7 @@ describe('supports http with nodejs', function () {
assert.equal(matches.length, 1, err.stack) assert.equal(matches.length, 1, err.stack)
return true; return true;
} }
).then(done).catch(done); )
});
}); });
it('should wrap interceptor errors and keep stack', function (done) { it('should wrap interceptor errors and keep stack', function (done) {