MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / instrumentFetch

Function instrumentFetch

packages/core/src/instrument/fetch.ts:49–157  ·  view source on GitHub ↗
(onFetchResolved?: (response: Response) => void, skipNativeFetchCheck: boolean = false)

Source from the content-addressed store, hash-verified

47}
48
49function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNativeFetchCheck: boolean = false): void {
50 if (skipNativeFetchCheck && !supportsNativeFetch()) {
51 return;
52 }
53
54 fill(GLOBAL_OBJ, 'fetch', function (originalFetch: () => void): () => void {
55 return function (...args: any[]): void {
56 // We capture the error right here and not in the Promise error callback because Safari (and probably other
57 // browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
58
59 // NOTE: If you are a Sentry user, and you are seeing this stack frame,
60 // it means the error, that was caused by your fetch call did not
61 // have a stack trace, so the SDK backfilled the stack trace so
62 // you can see which fetch call failed.
63 const virtualError = new Error();
64
65 const { method, url } = parseFetchArgs(args);
66 const handlerData: HandlerDataFetch = {
67 args,
68 fetchData: {
69 method,
70 url,
71 },
72 startTimestamp: timestampInSeconds() * 1000,
73 // // Adding the error to be able to fingerprint the failed fetch event in HttpClient instrumentation
74 virtualError,
75 headers: getHeadersFromFetchArgs(args),
76 };
77
78 // if there is no callback, fetch is instrumented directly
79 if (!onFetchResolved) {
80 triggerHandlers('fetch', {
81 ...handlerData,
82 });
83 }
84
85 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
86 return originalFetch.apply(GLOBAL_OBJ, args).then(
87 async (response: Response) => {
88 if (onFetchResolved) {
89 onFetchResolved(response);
90 } else {
91 triggerHandlers('fetch', {
92 ...handlerData,
93 endTimestamp: timestampInSeconds() * 1000,
94 response,
95 });
96 }
97
98 return response;
99 },
100 (error: Error) => {
101 triggerHandlers('fetch', {
102 ...handlerData,
103 endTimestamp: timestampInSeconds() * 1000,
104 error,
105 });
106

Calls 12

supportsNativeFetchFunction · 0.90
fillFunction · 0.90
timestampInSecondsFunction · 0.90
triggerHandlersFunction · 0.90
isErrorFunction · 0.90
addNonEnumerablePropertyFunction · 0.90
getClientFunction · 0.90
parseFetchArgsFunction · 0.85
getHeadersFromFetchArgsFunction · 0.85
getOptionsMethod · 0.65
thenMethod · 0.45
applyMethod · 0.45

Tested by

no test coverage detected