Skip to content

fix(browser): Fix parenthesis parsing logic for chromium #12373

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
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = [
path: 'packages/browser/build/npm/esm/index.js',
import: createImport('init', 'browserTracingIntegration', 'replayIntegration'),
gzip: true,
limit: '70 KB',
limit: '71 KB',
},
{
name: '@sentry/browser (incl. Tracing, Replay) - with treeshaking flags',
Expand Down
9 changes: 9 additions & 0 deletions packages/browser/src/stack-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,22 @@ function createFrame(filename: string, func: string, lineno?: number, colno?: nu
}

// Chromium based browsers: Chrome, Brave, new Opera, new Edge
const chromeRegexNoFnName = /^\s*at (\S+?)(?::(\d+))(?::(\d+))\s*$/i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add a comment here for what this would match, as an example? makes it easier to follow this than to try to parse the regex 😅

const chromeRegex =
/^\s*at (?:(.+?\)(?: \[.+\])?|.*?) ?\((?:address at )?)?(?:async )?((?:<anonymous>|[-a-z]+:|.*bundle|\/)?.*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i;
const chromeEvalRegex = /\((\S*)(?::(\d+))(?::(\d+))\)/;

// We cannot call this variable `chrome` because it can conflict with global `chrome` variable in certain environments
// See: https://github.com/getsentry/sentry-javascript/issues/6880
const chromeStackParserFn: StackLineParserFn = line => {
// If the stack line has no function name, we need to parse it differently
const noFnParts = chromeRegexNoFnName.exec(line);

if (noFnParts) {
const [, filename, line, col] = noFnParts;
return createFrame(filename, UNKNOWN_FUNCTION, +line, +col);
}

const parts = chromeRegex.exec(line);

if (parts) {
Expand Down
8 changes: 8 additions & 0 deletions packages/browser/test/unit/tracekit/chromium.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ describe('Tracekit - Chrome Tests', () => {
name: 'Error',
stack: `Error: bad
at something (http://localhost:5000/(some)/(thing)/index.html:20:16)
at http://localhost:5000/(group)/[route]/script.js:1:126
at more (http://localhost:5000/(some)/(thing)/index.html:25:7)`,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this could be passed?

at (http://localhost:5000/(group)/[route]/script.js:1:126)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought but chrome doesn't emit such frames I think.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't seen stack lines like that. They only appear to be surrounded in parenthesis when there is filename.

};

Expand All @@ -596,6 +597,13 @@ describe('Tracekit - Chrome Tests', () => {
colno: 7,
in_app: true,
},
{
filename: 'http://localhost:5000/(group)/[route]/script.js',
function: '?',
lineno: 1,
colno: 126,
in_app: true,
},
{
filename: 'http://localhost:5000/(some)/(thing)/index.html',
function: 'something',
Expand Down
35 changes: 35 additions & 0 deletions packages/browser/test/unit/tracekit/firefox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,41 @@ describe('Tracekit - Firefox Tests', () => {
});
});

it('should correctly parse parentheses', () => {
const PARENTHESIS_FRAME_EXCEPTION = {
message: 'aha',
name: 'Error',
stack:
'onClick@http://localhost:3002/_next/static/chunks/app/(group)/[route]/script.js:1:644\n' +
'@http://localhost:3002/_next/static/chunks/app/(group)/[route]/script.js:1:126',
};

const stacktrace = exceptionFromError(parser, PARENTHESIS_FRAME_EXCEPTION);

expect(stacktrace).toEqual({
value: 'aha',
type: 'Error',
stacktrace: {
frames: [
{
colno: 126,
filename: 'http://localhost:3002/_next/static/chunks/app/(group)/[route]/script.js',
function: '?',
in_app: true,
lineno: 1,
},
{
colno: 644,
filename: 'http://localhost:3002/_next/static/chunks/app/(group)/[route]/script.js',
function: 'onClick',
in_app: true,
lineno: 1,
},
],
},
});
});

it('should parse Firefox errors with `file` inside an identifier', () => {
const FIREFOX_FILE_IN_IDENTIFIER = {
stack:
Expand Down
35 changes: 35 additions & 0 deletions packages/browser/test/unit/tracekit/safari.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,39 @@ describe('Tracekit - Safari Tests', () => {
},
});
});

it('should correctly parse parentheses', () => {
const PARENTHESIS_FRAME_EXCEPTION = {
message: 'aha',
name: 'Error',
stack:
'@http://localhost:3000/(group)/[route]/script.js:1:131\n' +
'global code@http://localhost:3000/(group)/[route]/script.js:1:334',
};

const ex = exceptionFromError(parser, PARENTHESIS_FRAME_EXCEPTION);

expect(ex).toEqual({
value: 'aha',
type: 'Error',
stacktrace: {
frames: [
{
colno: 334,
filename: 'http://localhost:3000/(group)/[route]/script.js',
function: 'global code',
in_app: true,
lineno: 1,
},
{
colno: 131,
filename: 'http://localhost:3000/(group)/[route]/script.js',
function: '?',
in_app: true,
lineno: 1,
},
],
},
});
});
});
Loading