Skip to content

Commit a34dab6

Browse files
authored
ref(utils): use test instead of indexof in stacktrace (#7417)
1 parent 2f3c93c commit a34dab6

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

packages/utils/src/stacktrace.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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 && /sentryWrapped/.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 && /captureMessage|captureException/.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

9394
const defaultFunctionName = '<anonymous>';

0 commit comments

Comments
 (0)