Skip to content

Commit 03cfc4b

Browse files
authored
feat: capture error stack traces (nodejs#1619)
1 parent 6a87bfb commit 03cfc4b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

index-fetch.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ const fetchImpl = require('./lib/fetch')
55

66
module.exports.fetch = async function fetch (resource) {
77
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
8-
return fetchImpl.apply(dispatcher, arguments)
8+
try {
9+
return await fetchImpl.apply(dispatcher, arguments)
10+
} catch (err) {
11+
Error.captureStackTrace(err, this)
12+
throw err
13+
}
914
}
1015
module.exports.FormData = require('./lib/fetch/formdata').FormData
1116
module.exports.Headers = require('./lib/fetch/headers').Headers

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
9292
fetchImpl = require('./lib/fetch')
9393
}
9494
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
95-
return fetchImpl.apply(dispatcher, arguments)
95+
try {
96+
return await fetchImpl.apply(dispatcher, arguments)
97+
} catch (err) {
98+
Error.captureStackTrace(err, this)
99+
throw err
100+
}
96101
}
97102
module.exports.Headers = require('./lib/fetch/headers').Headers
98103
module.exports.Response = require('./lib/fetch/response').Response
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const { test } = require('tap')
4+
const { fetch } = require('../..')
5+
const { fetch: fetchIndex } = require('../../index-fetch')
6+
7+
test('FETCH: request errors and prints trimmed stack trace', async (t) => {
8+
try {
9+
await fetch('http://a.com')
10+
} catch (error) {
11+
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
12+
}
13+
})
14+
15+
test('FETCH-index: request errors and prints trimmed stack trace', async (t) => {
16+
try {
17+
await fetchIndex('http://a.com')
18+
} catch (error) {
19+
t.match(error.stack, `at Test.<anonymous> (${__filename}`)
20+
}
21+
})

0 commit comments

Comments
 (0)