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
10 changes: 9 additions & 1 deletion index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global')
const EnvHttpProxyAgent = require('./lib/dispatcher/env-http-proxy-agent')
const fetchImpl = require('./lib/web/fetch').fetch

// Capture __filename at module load time for stack trace augmentation.
// This may be undefined when bundled in environments like Node.js internals.
const currentFilename = typeof __filename !== 'undefined' ? __filename : undefined

function appendFetchStackTrace (err, filename) {
if (!err || typeof err !== 'object') {
return
Expand All @@ -30,7 +34,11 @@ function appendFetchStackTrace (err, filename) {

module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
appendFetchStackTrace(err, __filename)
if (currentFilename) {
appendFetchStackTrace(err, currentFilename)
} else if (err && typeof err === 'object') {
Error.captureStackTrace(err, module.exports.fetch)
}
throw err
})
}
Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ module.exports.getGlobalDispatcher = getGlobalDispatcher

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

// Capture __filename at module load time for stack trace augmentation.
// This may be undefined when bundled in environments like Node.js internals.
const currentFilename = typeof __filename !== 'undefined' ? __filename : undefined

function appendFetchStackTrace (err, filename) {
if (!err || typeof err !== 'object') {
return
Expand All @@ -147,7 +151,11 @@ function appendFetchStackTrace (err, filename) {

module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
appendFetchStackTrace(err, __filename)
if (currentFilename) {
appendFetchStackTrace(err, currentFilename)
} else if (err && typeof err === 'object') {
Error.captureStackTrace(err, module.exports.fetch)
}
throw err
})
}
Expand Down
Loading