aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/setupFetchDebugging.js
blob: dd87b65a617bb288399268866fe6f7b06bb63e5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module.exports = setupFetchDebugging

//
// 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
//

function setupFetchDebugging() {
  if (!global.fetch) return
  const originalFetch = global.fetch

  global.fetch = wrappedFetch

  async function wrappedFetch(...args) {
    const initialStack = getCurrentStack()
    try {
      return await originalFetch.call(window, ...args)
    } catch (err) {
      console.warn('FetchDebugger - fetch encountered an Error', 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
  }
}