Skip to content

Commit

Permalink
ref(utils): use test instead of indexof
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Mar 10, 2023
1 parent 63ef40e commit b6bbf0f
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,31 @@ export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {

let localStack = stack;

const firstFrameFunction = localStack[0].function || '';
const lastFrameFunction = localStack[localStack.length - 1].function || '';

// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
if (firstFrameFunction.indexOf('captureMessage') !== -1 || firstFrameFunction.indexOf('captureException') !== -1) {
localStack = localStack.slice(1);
if (stack.length >= STACKTRACE_LIMIT) {
localStack.slice(0, STACKTRACE_LIMIT);
}

const lastFrameFunction = localStack[localStack.length - 1].function;
// 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)
if (lastFrameFunction.indexOf('sentryWrapped') !== -1) {
localStack = localStack.slice(0, -1);
if (lastFrameFunction && /sentryWrapped/.test(lastFrameFunction)) {
localStack.pop();
}

// Reversing in the middle of the procedure allows us to just pop the values off the stack
localStack = localStack.reverse();

const firstFrameFunction = localStack[localStack.length - 1].function;
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
if (firstFrameFunction && /captureMessage|captureException/.test(firstFrameFunction)) {
localStack.pop();
}

// The frame where the crash happened, should be the last entry in the array
return localStack
.slice(0, STACKTRACE_LIMIT)
.map(frame => ({
...frame,
filename: frame.filename || localStack[0].filename,
function: frame.function || '?',
}))
.reverse();
return localStack.map(frame => ({
...frame,
filename: frame.filename || localStack[0].filename,
function: frame.function || '?',
}));
}

const defaultFunctionName = '<anonymous>';
Expand Down

0 comments on commit b6bbf0f

Please sign in to comment.