Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@ const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent')
const fetchImpl = require('./lib/web/fetch').fetch

function appendFetchStackTrace (err, filename) {
if (!err || typeof err !== 'object') {
return
}

const stack = typeof err.stack === 'string' ? err.stack : ''
const normalizedFilename = filename.replace(/\\/g, '/')

if (stack && (stack.includes(filename) || stack.includes(normalizedFilename))) {
return
}

const capture = {}
Error.captureStackTrace(capture, appendFetchStackTrace)

if (!capture.stack) {
return
}

const captureLines = capture.stack.split('\n').slice(1).join('\n')

err.stack = stack ? `${stack}\n${captureLines}` : capture.stack
}

module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
if (err && typeof err === 'object') {
Error.captureStackTrace(err)
}
appendFetchStackTrace(err, __filename)
throw err
})
}
Expand Down
28 changes: 25 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,33 @@ module.exports.getGlobalDispatcher = getGlobalDispatcher

const fetchImpl = require('./lib/web/fetch').fetch

function appendFetchStackTrace (err, filename) {
if (!err || typeof err !== 'object') {
return
}

const stack = typeof err.stack === 'string' ? err.stack : ''
const normalizedFilename = filename.replace(/\\/g, '/')

if (stack && (stack.includes(filename) || stack.includes(normalizedFilename))) {
return
}

const capture = {}
Error.captureStackTrace(capture, appendFetchStackTrace)

if (!capture.stack) {
return
}

const captureLines = capture.stack.split('\n').slice(1).join('\n')

err.stack = stack ? `${stack}\n${captureLines}` : capture.stack
}

module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
if (err && typeof err === 'object') {
Error.captureStackTrace(err)
}
appendFetchStackTrace(err, __filename)
throw err
})
}
Expand Down
12 changes: 6 additions & 6 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ test('FETCH: request errors and prints trimmed stack trace', async (t) => {
} catch (error) {
const stackLines = error.stack.split('\n')
t.assert.ok(stackLines[0].includes('TypeError: fetch failed'))
t.assert.ok(stackLines[1].includes(`${projectFolder}${sep}index.js`))
t.assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
t.assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
t.assert.ok(stackLines.some(line => line.includes(`lib${sep}web${sep}fetch${sep}index.js`)))
t.assert.ok(stackLines.some(line => line.includes(`${projectFolder}${sep}index.js`)))
t.assert.ok(stackLines.some(line => line.includes(__filename)))
}
})

Expand All @@ -30,8 +30,8 @@ test('FETCH-index: request errors and prints trimmed stack trace', async (t) =>
} catch (error) {
const stackLines = error.stack.split('\n')
t.assert.ok(stackLines[0].includes('TypeError: fetch failed'))
t.assert.ok(stackLines[1].includes(`${projectFolder}${sep}index-fetch.js`))
t.assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
t.assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
t.assert.ok(stackLines.some(line => line.includes(`lib${sep}web${sep}fetch${sep}index.js`)))
t.assert.ok(stackLines.some(line => line.includes(`${projectFolder}${sep}index-fetch.js`)))
t.assert.ok(stackLines.some(line => line.includes(__filename)))
}
})
Loading