2018-10-15 07:14:25 +02:00
|
|
|
//
|
|
|
|
// This is a utility to help resolve cases where `window.fetch` throws a
|
2018-10-19 12:42:53 +02:00
|
|
|
// `TypeError: Failed to Fetch` without any stack or context for the request
|
2018-10-15 07:14:25 +02:00
|
|
|
// https://github.com/getsentry/sentry-javascript/pull/1293
|
|
|
|
//
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function setupFetchDebugging() {
|
2020-04-15 19:23:27 +02:00
|
|
|
if (!window.fetch) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
const originalFetch = window.fetch;
|
2018-10-15 07:14:25 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
window.fetch = wrappedFetch;
|
2018-10-15 07:14:25 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function wrappedFetch(...args) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const initialStack = getCurrentStack();
|
2018-10-15 07:14:25 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
return await originalFetch.call(window, ...args);
|
2018-10-15 07:14:25 +02:00
|
|
|
} catch (err) {
|
2018-10-19 12:42:53 +02:00
|
|
|
if (!err.stack) {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.warn(
|
|
|
|
'FetchDebugger - fetch encountered an Error without a stack',
|
|
|
|
err,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
console.warn(
|
|
|
|
'FetchDebugger - overriding stack to point of original call',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
err.stack = initialStack;
|
2018-10-19 12:42:53 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
throw err;
|
2018-10-15 07:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function getCurrentStack() {
|
2018-10-15 07:14:25 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Fake error for generating stack trace');
|
2018-10-15 07:14:25 +02:00
|
|
|
} catch (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return err.stack;
|
2018-10-15 07:14:25 +02:00
|
|
|
}
|
|
|
|
}
|