Skip to content
Open
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
2 changes: 1 addition & 1 deletion index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.fetch = function fetch (init, options = undefined) {
if (currentFilename) {
appendFetchStackTrace(err, currentFilename)
} else if (err && typeof err === 'object') {
Error.captureStackTrace(err, module.exports.fetch)
Error.captureStackTrace(err)
}
throw err
})
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ module.exports.fetch = function fetch (init, options = undefined) {
if (currentFilename) {
appendFetchStackTrace(err, currentFilename)
} else if (err && typeof err === 'object') {
Error.captureStackTrace(err, module.exports.fetch)
Error.captureStackTrace(err)
}
throw err
})
Expand Down
18 changes: 18 additions & 0 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const { test, after } = require('node:test')
const { sep, basename, join } = require('node:path')
const { readFileSync } = require('node:fs')
const { createRequire } = require('node:module')
const { runInThisContext } = require('node:vm')
const { fetch, setGlobalDispatcher, getGlobalDispatcher, Agent } = require('../..')

const projectFolder = basename(join(__dirname, '..', '..'))
Expand Down Expand Up @@ -40,3 +43,18 @@ test('FETCH-index: request errors and prints trimmed stack trace', async (t) =>
t.assert.ok(stackLines.some(line => line.includes(__filename)))
}
})

test('FETCH-index: request errors keep their stack trace when __filename is undefined', async (t) => {
// Node.js bundles index-fetch.js into a module wrapper without __filename
const file = join(__dirname, '..', '..', 'index-fetch.js')
const wrapper = runInThisContext(`(function (exports, require, module) {${readFileSync(file, 'utf8')}\n})`, { filename: file })
const bundled = { exports: {} }
wrapper(bundled.exports, createRequire(file), bundled)

await t.assert.rejects(bundled.exports.fetch('http://localhost:1'), (error) => {
const stackLines = error.stack.split('\n')
t.assert.ok(stackLines[0].includes('TypeError: fetch failed'))
t.assert.ok(stackLines.some(line => line.includes(__filename)))
return true
})
})