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

module.exports.fetch = function fetch (resource, init = undefined) {
return fetchImpl(resource, init).catch((err) => {
module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
if (err && typeof err === 'object') {
Error.captureStackTrace(err)
}
Expand Down
10 changes: 4 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,14 @@ module.exports.setGlobalDispatcher = setGlobalDispatcher
module.exports.getGlobalDispatcher = getGlobalDispatcher

const fetchImpl = require('./lib/web/fetch').fetch
module.exports.fetch = async function fetch (init, options = undefined) {
try {
return await fetchImpl(init, options)
} catch (err) {

module.exports.fetch = function fetch (init, options = undefined) {
return fetchImpl(init, options).catch(err => {
if (err && typeof err === 'object') {
Error.captureStackTrace(err)
}

throw err
}
})
}
module.exports.Headers = require('./lib/web/fetch/headers').Headers
module.exports.Response = require('./lib/web/fetch/response').Response
Expand Down
13 changes: 11 additions & 2 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { test } = require('node:test')
const assert = require('node:assert')
const { sep } = require('node:path')
const { fetch, setGlobalDispatcher, Agent } = require('../..')
const { fetch: fetchIndex } = require('../../index-fetch')

Expand All @@ -14,14 +15,22 @@ test('FETCH: request errors and prints trimmed stack trace', async (t) => {
try {
await fetch('http://a.com')
} catch (error) {
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
const stackLines = error.stack.split('\n')
assert.ok(stackLines[0].includes('TypeError: fetch failed'))
assert.ok(stackLines[1].includes(`undici${sep}index.js`))
assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
}
})

test('FETCH-index: request errors and prints trimmed stack trace', async (t) => {
try {
await fetchIndex('http://a.com')
} catch (error) {
assert.ok(error.stack.includes(`at async TestContext.<anonymous> (${__filename}`))
const stackLines = error.stack.split('\n')
assert.ok(stackLines[0].includes('TypeError: fetch failed'))
assert.ok(stackLines[1].includes(`undici${sep}index-fetch.js`))
assert.ok(stackLines[2].includes('at process.processTicksAndRejections'))
assert.ok(stackLines[3].includes(`at async TestContext.<anonymous> (${__filename}`))
}
})
Loading