1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/app/scripts/lib/setupFetchDebugging.js

42 lines
1012 B
JavaScript
Raw Normal View History

2018-10-15 07:14:25 +02:00
//
// This is a utility to help resolve cases where `window.fetch` throws a
// `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() {
if (!window.fetch) {
return;
}
const originalFetch = window.fetch;
2018-10-15 07:14:25 +02:00
window.fetch = wrappedFetch;
2018-10-15 07:14:25 +02:00
2020-11-03 00:41:28 +01:00
async function wrappedFetch(...args) {
const initialStack = getCurrentStack();
2018-10-15 07:14:25 +02:00
try {
return await originalFetch.call(window, ...args);
2018-10-15 07:14:25 +02:00
} catch (err) {
if (!err.stack) {
2020-11-03 00:41:28 +01:00
console.warn(
'FetchDebugger - fetch encountered an Error without a stack',
err,
);
2020-11-03 00:41:28 +01:00
console.warn(
'FetchDebugger - overriding stack to point of original call',
);
err.stack = initialStack;
}
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 {
throw new Error('Fake error for generating stack trace');
2018-10-15 07:14:25 +02:00
} catch (err) {
return err.stack;
2018-10-15 07:14:25 +02:00
}
}