@@ -106,10 +106,11 @@ export function parseStackFrames(
106106 // reliably in other circumstances.
107107 const stacktrace = ex . stacktrace || ex . stack || '' ;
108108
109- const popSize = getPopSize ( ex ) ;
109+ const skipLines = getSkipFirstStackStringLines ( ex ) ;
110+ const framesToPop = getPopFirstTopFrames ( ex ) ;
110111
111112 try {
112- return stackParser ( stacktrace , popSize ) ;
113+ return stackParser ( stacktrace , skipLines , framesToPop ) ;
113114 } catch ( e ) {
114115 // no-empty
115116 }
@@ -120,15 +121,30 @@ export function parseStackFrames(
120121// Based on our own mapping pattern - https://github.com/getsentry/sentry/blob/9f08305e09866c8bd6d0c24f5b0aabdd7dd6c59c/src/sentry/lang/javascript/errormapping.py#L83-L108
121122const reactMinifiedRegexp = / M i n i f i e d R e a c t e r r o r # \d + ; / i;
122123
123- function getPopSize ( ex : Error & { framesToPop ?: number } ) : number {
124- if ( ex ) {
125- if ( typeof ex . framesToPop === 'number' ) {
126- return ex . framesToPop ;
127- }
124+ /**
125+ * Certain known React errors contain links that would be falsely
126+ * parsed as frames. This function check for these errors and
127+ * returns number of the stack string lines to skip.
128+ */
129+ function getSkipFirstStackStringLines ( ex : Error ) : number {
130+ if ( ex && reactMinifiedRegexp . test ( ex . message ) ) {
131+ return 1 ;
132+ }
128133
129- if ( reactMinifiedRegexp . test ( ex . message ) ) {
130- return 1 ;
131- }
134+ return 0 ;
135+ }
136+
137+ /**
138+ * If error has `framesToPop` property, it means that the
139+ * creator tells us the first x frames will be useless
140+ * and should be discarded. Typically error from wrapper function
141+ * which don't point to the actual location in the developer's code.
142+ *
143+ * Example: https://github.com/zertosh/invariant/blob/master/invariant.js#L46
144+ */
145+ function getPopFirstTopFrames ( ex : Error & { framesToPop ?: unknown } ) : number {
146+ if ( typeof ex . framesToPop === 'number' ) {
147+ return ex . framesToPop ;
132148 }
133149
134150 return 0 ;
0 commit comments