From ec6f007852ce3445441c35e027f3a2782aaa3266 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sat, 27 Aug 2022 01:13:53 +0530 Subject: [PATCH 1/3] feat: capture error stack traces --- index-fetch.js | 7 ++++++- index.js | 7 ++++++- test/client-error-stack-trace.js | 12 ++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 test/client-error-stack-trace.js diff --git a/index-fetch.js b/index-fetch.js index 2ee12fc1ef7..00fe6ea162f 100644 --- a/index-fetch.js +++ b/index-fetch.js @@ -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 diff --git a/index.js b/index.js index 8099f5a692f..284661215c3 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/client-error-stack-trace.js b/test/client-error-stack-trace.js new file mode 100644 index 00000000000..ae0f982ddf2 --- /dev/null +++ b/test/client-error-stack-trace.js @@ -0,0 +1,12 @@ +'use strict' + +const { test } = require('tap') +const { fetch } = require('..') + +test('Request errors and prints trimmed stack trace', async (t) => { + try { + await fetch('http://a.com') + } catch (error) { + t.match(error.stack, `at Test. (${__filename}`) + } +}) From 69a28ac4791976797d9bccb7dbb3717c354e718f Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sat, 27 Aug 2022 02:22:18 +0530 Subject: [PATCH 2/3] test: add tests for index-fetch.js --- test/client-error-stack-trace.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/client-error-stack-trace.js b/test/client-error-stack-trace.js index ae0f982ddf2..d2f92434184 100644 --- a/test/client-error-stack-trace.js +++ b/test/client-error-stack-trace.js @@ -2,11 +2,20 @@ const { test } = require('tap') const { fetch } = require('..') +const { fetch: fetchIndex } = require('../index-fetch') -test('Request errors and prints trimmed stack trace', async (t) => { +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. (${__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. (${__filename}`) + } +}) From b99ede4c0c5b8275d3308c9f5cd2152e94d6d950 Mon Sep 17 00:00:00 2001 From: Sidharth Rathi Date: Sat, 27 Aug 2022 03:40:15 +0530 Subject: [PATCH 3/3] test: move test file to test/fetch --- test/{ => fetch}/client-error-stack-trace.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename test/{ => fetch}/client-error-stack-trace.js (83%) diff --git a/test/client-error-stack-trace.js b/test/fetch/client-error-stack-trace.js similarity index 83% rename from test/client-error-stack-trace.js rename to test/fetch/client-error-stack-trace.js index d2f92434184..7d94aa8b73c 100644 --- a/test/client-error-stack-trace.js +++ b/test/fetch/client-error-stack-trace.js @@ -1,8 +1,8 @@ 'use strict' const { test } = require('tap') -const { fetch } = require('..') -const { fetch: fetchIndex } = require('../index-fetch') +const { fetch } = require('../..') +const { fetch: fetchIndex } = require('../../index-fetch') test('FETCH: request errors and prints trimmed stack trace', async (t) => { try {