Skip to content

fix(v8/utils): Stack parser skip frames (not lines of stack string) #10560

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

Merged
merged 6 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 26 additions & 10 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ export function parseStackFrames(
// reliably in other circumstances.
const stacktrace = ex.stacktrace || ex.stack || '';

const popSize = getPopSize(ex);
const skipLines = getSkipFirstStackStringLines(ex);
const framesToPop = getPopFirstTopFrames(ex);

try {
return stackParser(stacktrace, popSize);
return stackParser(stacktrace, skipLines, framesToPop);
} catch (e) {
// no-empty
}
Expand All @@ -120,15 +121,30 @@ export function parseStackFrames(
// Based on our own mapping pattern - https://github.com/getsentry/sentry/blob/9f08305e09866c8bd6d0c24f5b0aabdd7dd6c59c/src/sentry/lang/javascript/errormapping.py#L83-L108
const reactMinifiedRegexp = /Minified React error #\d+;/i;

function getPopSize(ex: Error & { framesToPop?: number }): number {
if (ex) {
if (typeof ex.framesToPop === 'number') {
return ex.framesToPop;
}
/**
* Certain known React errors contain links that would be falsely
* parsed as frames. This function check for these errors and
* returns number of the stack string lines to skip.
*/
function getSkipFirstStackStringLines(ex: Error): number {
if (ex && reactMinifiedRegexp.test(ex.message)) {
return 1;
}

if (reactMinifiedRegexp.test(ex.message)) {
return 1;
}
return 0;
}

/**
* If error has `framesToPop` property, it means that the
* creator tells us the first x frames will be useless
* and should be discarded. Typically error from wrapper function
* which don't point to the actual location in the developer's code.
*
* Example: https://github.com/zertosh/invariant/blob/master/invariant.js#L46
*/
function getPopFirstTopFrames(ex: Error & { framesToPop?: unknown }): number {
if (typeof ex.framesToPop === 'number') {
return ex.framesToPop;
}

return 0;
Expand Down
18 changes: 2 additions & 16 deletions packages/browser/test/unit/tracekit/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exceptionFromError } from '../../../src/eventbuilder';
import { defaultStackParser as parser } from '../../../src/stack-parsers';

describe('Tracekit - React Tests', () => {
it('should correctly parse Invariant Violation errors and use framesToPop to drop info message', () => {
it('should correctly parse Invariant Violation errors and use framesToPop to drop the invariant frame', () => {
const REACT_INVARIANT_VIOLATION_EXCEPTION = {
framesToPop: 1,
message:
Expand Down Expand Up @@ -38,13 +38,6 @@ describe('Tracekit - React Tests', () => {
colno: 21841,
in_app: true,
},
{
filename: 'http://localhost:5000/static/js/foo.chunk.js',
function: '?',
lineno: 1,
colno: 21738,
in_app: true,
},
],
},
});
Expand Down Expand Up @@ -97,7 +90,7 @@ describe('Tracekit - React Tests', () => {
});
});

it('should not drop additional frame for production errors if framesToPop is still there', () => {
it('should drop invariant frame for production errors if framesToPop is present', () => {
const REACT_PRODUCTION_ERROR = {
framesToPop: 1,
message:
Expand Down Expand Up @@ -133,13 +126,6 @@ describe('Tracekit - React Tests', () => {
colno: 21841,
in_app: true,
},
{
filename: 'http://localhost:5000/static/js/foo.chunk.js',
function: '?',
lineno: 1,
colno: 21738,
in_app: true,
},
],
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/stacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export interface Stacktrace {
frames_omitted?: [number, number];
}

export type StackParser = (stack: string, skipFirst?: number) => StackFrame[];
export type StackParser = (stack: string, skipFirstLines?: number, framesToPop?: number) => StackFrame[];
export type StackLineParserFn = (line: string) => StackFrame | undefined;
export type StackLineParser = [number, StackLineParserFn];
8 changes: 4 additions & 4 deletions packages/utils/src/stacktrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const STRIP_FRAME_REGEXP = /captureMessage|captureException/;
export function createStackParser(...parsers: StackLineParser[]): StackParser {
const sortedParsers = parsers.sort((a, b) => a[0] - b[0]).map(p => p[1]);

return (stack: string, skipFirst: number = 0): StackFrame[] => {
return (stack: string, skipFirstLines: number = 0, framesToPop: number = 0): StackFrame[] => {
const frames: StackFrame[] = [];
const lines = stack.split('\n');

for (let i = skipFirst; i < lines.length; i++) {
for (let i = skipFirstLines; i < lines.length; i++) {
const line = lines[i];
// Ignore lines over 1kb as they are unlikely to be stack frames.
// Many of the regular expressions use backtracking which results in run time that increases exponentially with
Expand Down Expand Up @@ -53,12 +53,12 @@ export function createStackParser(...parsers: StackLineParser[]): StackParser {
}
}

if (frames.length >= STACKTRACE_FRAME_LIMIT) {
if (frames.length >= STACKTRACE_FRAME_LIMIT + framesToPop) {
break;
}
}

return stripSentryFramesAndReverse(frames);
return stripSentryFramesAndReverse(frames.slice(framesToPop));
};
}

Expand Down