11/* eslint-disable @typescript-eslint/no-explicit-any */
22import type { HandlerDataFetch } from '@sentry/types' ;
33
4- import { fill } from '../object' ;
4+ import { isError } from '../is' ;
5+ import { addNonEnumerableProperty , fill } from '../object' ;
56import { supportsNativeFetch } from '../supports' ;
67import { timestampInSeconds } from '../time' ;
78import { GLOBAL_OBJ } from '../worldwide' ;
@@ -45,6 +46,15 @@ function instrumentFetch(): void {
4546 ...handlerData ,
4647 } ) ;
4748
49+ // We capture the stack right here and not in the Promise error callback because Safari (and probably other
50+ // browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
51+
52+ // NOTE: If you are a Sentry user, and you are seeing this stack frame,
53+ // it means the error, that was caused by your fetch call did not
54+ // have a stack trace, so the SDK backfilled the stack trace so
55+ // you can see which fetch call failed.
56+ const virtualStackTrace = new Error ( ) . stack ;
57+
4858 // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
4959 return originalFetch . apply ( GLOBAL_OBJ , args ) . then (
5060 ( response : Response ) => {
@@ -65,6 +75,16 @@ function instrumentFetch(): void {
6575 } ;
6676
6777 triggerHandlers ( 'fetch' , erroredHandlerData ) ;
78+
79+ if ( isError ( error ) && error . stack === undefined ) {
80+ // NOTE: If you are a Sentry user, and you are seeing this stack frame,
81+ // it means the error, that was caused by your fetch call did not
82+ // have a stack trace, so the SDK backfilled the stack trace so
83+ // you can see which fetch call failed.
84+ error . stack = virtualStackTrace ;
85+ addNonEnumerableProperty ( error , 'framesToPop' , 1 ) ;
86+ }
87+
6888 // NOTE: If you are a Sentry user, and you are seeing this stack frame,
6989 // it means the sentry.javascript SDK caught an error invoking your application code.
7090 // This is expected behavior and NOT indicative of a bug with sentry.javascript.
0 commit comments