1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
2
import type { HandlerDataFetch } from '@sentry/types' ;
3
3
4
- import { fill } from '../object' ;
4
+ import { isError } from '../is' ;
5
+ import { addNonEnumerableProperty , fill } from '../object' ;
5
6
import { supportsNativeFetch } from '../supports' ;
6
7
import { timestampInSeconds } from '../time' ;
7
8
import { GLOBAL_OBJ } from '../worldwide' ;
@@ -45,6 +46,15 @@ function instrumentFetch(): void {
45
46
...handlerData ,
46
47
} ) ;
47
48
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
+
48
58
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
49
59
return originalFetch . apply ( GLOBAL_OBJ , args ) . then (
50
60
( response : Response ) => {
@@ -65,6 +75,16 @@ function instrumentFetch(): void {
65
75
} ;
66
76
67
77
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
+
68
88
// NOTE: If you are a Sentry user, and you are seeing this stack frame,
69
89
// it means the sentry.javascript SDK caught an error invoking your application code.
70
90
// This is expected behavior and NOT indicative of a bug with sentry.javascript.
0 commit comments