Skip to content

Commit

Permalink
feat: capture error stack traces (nodejs#1619)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidwebworks authored and metcoder95 committed Dec 26, 2022
1 parent cc43209 commit 341cefe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ const fetchImpl = require('./lib/fetch')

module.exports.fetch = async function fetch (resource) {
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
return fetchImpl.apply(dispatcher, arguments)
try {
return await fetchImpl.apply(dispatcher, arguments)
} catch (err) {
Error.captureStackTrace(err, this)
throw err
}
}
module.exports.FormData = require('./lib/fetch/formdata').FormData
module.exports.Headers = require('./lib/fetch/headers').Headers
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
fetchImpl = require('./lib/fetch')
}
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
return fetchImpl.apply(dispatcher, arguments)
try {
return await fetchImpl.apply(dispatcher, arguments)
} catch (err) {
Error.captureStackTrace(err, this)
throw err
}
}
module.exports.Headers = require('./lib/fetch/headers').Headers
module.exports.Response = require('./lib/fetch/response').Response
Expand Down
21 changes: 21 additions & 0 deletions test/fetch/client-error-stack-trace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { test } = require('tap')
const { fetch } = require('../..')
const { fetch: fetchIndex } = require('../../index-fetch')

test('FETCH: request errors and prints trimmed stack trace', async (t) => {
try {
await fetch('http://a.com')
} catch (error) {
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
}
})

test('FETCH-index: request errors and prints trimmed stack trace', async (t) => {
try {
await fetchIndex('http://a.com')
} catch (error) {
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
}
})

0 comments on commit 341cefe

Please sign in to comment.