Skip to content

Commit b6bbf0f

Browse files
committed
ref(utils): use test instead of indexof
1 parent 63ef40e commit b6bbf0f

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

packages/utils/src/stacktrace.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,31 @@ export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {
6464

6565
let localStack = stack;
6666

67-
const firstFrameFunction = localStack[0].function || '';
68-
const lastFrameFunction = localStack[localStack.length - 1].function || '';
69-
70-
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
71-
if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
72-
localStack = localStack.slice(1);
67+
if (stack.length >= STACKTRACE_LIMIT) {
68+
localStack.slice(0, STACKTRACE_LIMIT);
7369
}
7470

71+
const lastFrameFunction = localStack[localStack.length - 1].function;
7572
// 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)
76-
if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
77-
localStack = localStack.slice(0, -1);
73+
if (lastFrameFunction && /sentryWrapped/.test(lastFrameFunction)) {
74+
localStack.pop();
75+
}
76+
77+
// Reversing in the middle of the procedure allows us to just pop the values off the stack
78+
localStack = localStack.reverse();
79+
80+
const firstFrameFunction = localStack[localStack.length - 1].function;
81+
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
82+
if (firstFrameFunction && /captureMessage|captureException/.test(firstFrameFunction)) {
83+
localStack.pop();
7884
}
7985

8086
// The frame where the crash happened, should be the last entry in the array
81-
return localStack
82-
.slice(0, STACKTRACE_LIMIT)
83-
.map(frame => ({
84-
...frame,
85-
filename: frame.filename || localStack[0].filename,
86-
function: frame.function || '?',
87-
}))
88-
.reverse();
87+
return localStack.map(frame => ({
88+
...frame,
89+
filename: frame.filename || localStack[0].filename,
90+
function: frame.function || '?',
91+
}));
8992
}
9093

9194
const defaultFunctionName = '<anonymous>';

0 commit comments

Comments
 (0)