1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 04:37:13 +01:00
metamask-extension/app/scripts/lib/setupFetchDebugging.js
Erik Marks 76a2a9bb8b
@metamask/eslint config@5.0.0 (#10358)
* @metamask/eslint-config@5.0.0
* Update eslintrc and prettierrc
* yarn lint:fix
2021-02-04 10:15:23 -08:00

42 lines
1012 B
JavaScript

//
// 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
// https://github.com/getsentry/sentry-javascript/pull/1293
//
export default function setupFetchDebugging() {
if (!window.fetch) {
return;
}
const originalFetch = window.fetch;
window.fetch = wrappedFetch;
async function wrappedFetch(...args) {
const initialStack = getCurrentStack();
try {
return await originalFetch.call(window, ...args);
} catch (err) {
if (!err.stack) {
console.warn(
'FetchDebugger - fetch encountered an Error without a stack',
err,
);
console.warn(
'FetchDebugger - overriding stack to point of original call',
);
err.stack = initialStack;
}
throw err;
}
}
}
function getCurrentStack() {
try {
throw new Error('Fake error for generating stack trace');
} catch (err) {
return err.stack;
}
}