Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(utils): use test instead of indexof in stacktrace #7417

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,37 +55,38 @@ export function stackParserFromStackParserOptions(stackParser: StackParser | Sta
}

/**
* Removes Sentry frames from the top and bottom of the stack if present and enforces a limit of max number of frames.
* Assumes stack input is ordered from top to bottom and returns the reverse representation so call site of the
* function that caused the crash is the last frame in the array.
* @hidden
*/
export function stripSentryFramesAndReverse(stack: StackFrame[]): StackFrame[] {
export function stripSentryFramesAndReverse(stack: ReadonlyArray<StackFrame>): StackFrame[] {
if (!stack.length) {
return [];
}

let localStack = stack;

const firstFrameFunction = localStack[0].function || '';
const lastFrameFunction = localStack[localStack.length - 1].function || '';
const localStack = stack.slice(0, STACKTRACE_LIMIT);

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 (lastFrameFunction && /sentryWrapped/.test(lastFrameFunction)) {
localStack.pop();
}

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

const firstFrameFunction = 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 (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[localStack.length - 1].filename,
function: frame.function || '?',
}));
}

const defaultFunctionName = '<anonymous>';
Expand Down