@@ -57,37 +57,38 @@ export function stackParserFromStackParserOptions(stackParser: StackParser | Sta
5757}
5858
5959/**
60+ * Removes Sentry frames from the top and bottom of the stack if present and enforces a limit of max number of frames.
61+ * Assumes stack input is ordered from top to bottom and returns the reverse representation so call site of the
62+ * function that caused the crash is the last frame in the array.
6063 * @hidden
6164 */
62- export function stripSentryFramesAndReverse ( stack : StackFrame [ ] ) : StackFrame [ ] {
65+ export function stripSentryFramesAndReverse ( stack : ReadonlyArray < StackFrame > ) : StackFrame [ ] {
6366 if ( ! stack . length ) {
6467 return [ ] ;
6568 }
6669
67- let localStack = stack ;
68-
69- const firstFrameFunction = localStack [ 0 ] . function || '' ;
70- const lastFrameFunction = localStack [ localStack . length - 1 ] . function || '' ;
70+ const localStack = stack . slice ( 0 , STACKTRACE_LIMIT ) ;
7171
72+ const lastFrameFunction = localStack [ localStack . length - 1 ] . function ;
7273 // If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
73- if ( firstFrameFunction . indexOf ( 'captureMessage' ) !== - 1 || firstFrameFunction . indexOf ( 'captureException' ) !== - 1 ) {
74- localStack = localStack . slice ( 1 ) ;
74+ if ( lastFrameFunction && / s e n t r y W r a p p e d / . test ( lastFrameFunction ) ) {
75+ localStack . pop ( ) ;
7576 }
7677
78+ // Reversing in the middle of the procedure allows us to just pop the values off the stack
79+ localStack . reverse ( ) ;
80+
81+ const firstFrameFunction = localStack [ localStack . length - 1 ] . function ;
7782 // If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
78- if ( lastFrameFunction . indexOf ( 'sentryWrapped' ) !== - 1 ) {
79- localStack = localStack . slice ( 0 , - 1 ) ;
83+ if ( firstFrameFunction && / c a p t u r e M e s s a g e | c a p t u r e E x c e p t i o n / . test ( firstFrameFunction ) ) {
84+ localStack . pop ( ) ;
8085 }
8186
82- // The frame where the crash happened, should be the last entry in the array
83- return localStack
84- . slice ( 0 , STACKTRACE_LIMIT )
85- . map ( frame => ( {
86- ...frame ,
87- filename : frame . filename || localStack [ 0 ] . filename ,
88- function : frame . function || '?' ,
89- } ) )
90- . reverse ( ) ;
87+ return localStack . map ( frame => ( {
88+ ...frame ,
89+ filename : frame . filename || localStack [ localStack . length - 1 ] . filename ,
90+ function : frame . function || '?' ,
91+ } ) ) ;
9192}
9293
9394const defaultFunctionName = '<anonymous>' ;
0 commit comments