-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nextjs): parses the nextjs router correctly #12321
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
298014f
fix(nextjs): parses the nextjs router correctly
dohooo 9a3d09a
chore: run format
dohooo b84d951
chore: add test for handling nextjs scenario
dohooo 694e56c
Merge branch 'develop' into fix/pases-nextjs-routes
dohooo 78df5ed
chore: solves comments
dohooo bfcbed9
chore: code formatting
dohooo 9b21f5a
Merge branch 'fix/pases-nextjs-routes' of https://github.com/dohooo/s…
dohooo c66c80a
chore: fix GitHub comments and add more cases for testing.
dohooo 1ff1cab
Merge branch 'develop' into fix/pases-nextjs-routes
dohooo 36a7b67
chore: formatting code
dohooo 67f2800
Merge branch 'develop' into fix/pases-nextjs-routes
dohooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { nextStackParser } from '@sentry/nextjs'; | ||
import { createStackParser } from '@sentry/utils'; | ||
import { exceptionFromError } from '../../../src/eventbuilder'; | ||
import { chromeStackLineParser, geckoStackLineParser, winjsStackLineParser } from '../../../src/stack-parsers'; | ||
|
||
const parser = createStackParser(nextStackParser, chromeStackLineParser, geckoStackLineParser, winjsStackLineParser); | ||
|
||
describe('Tracekit - Next.js Tests', () => { | ||
it('should parse Next.js routes error', () => { | ||
const NEXT = { | ||
name: 'foo', | ||
message: "Unable to get property 'undef' of undefined or null reference", | ||
stack: | ||
"TypeError: Unable to get property 'undef' of undefined or null reference\n" + | ||
' at http://localhost:3001/_next/static/chunks/app/[locale]/sentery-example-page/(default)/sub/page-3d428c1ba734e10f.js:1:126', | ||
description: "Unable to get property 'undef' of undefined or null reference", | ||
number: -2146823281, | ||
}; | ||
|
||
const ex = exceptionFromError(parser, NEXT); | ||
|
||
expect(ex).toEqual({ | ||
value: "Unable to get property 'undef' of undefined or null reference", | ||
type: 'foo', | ||
stacktrace: { | ||
frames: [ | ||
{ | ||
filename: | ||
'http://localhost:3001/_next/static/chunks/app/%5Blocale%5D/sentery-example-page/%28default%29/sub/page-3d428c1ba734e10f.js', | ||
function: '?', | ||
lineno: 1, | ||
colno: 126, | ||
in_app: true, | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { defaultStackLineParsers, defaultStackParser } from '@sentry/react'; | ||
import type { StackLineParser } from '@sentry/types'; | ||
|
||
// We should put it in the highest priority to make sure it will be executed first to handle the Next.js routes. | ||
const highestPriority = defaultStackLineParsers.sort((a, b) => a[0] - b[0])[0][0] - 1; | ||
|
||
/** | ||
* This stack parser is used to handle the special case of Next.js routes. | ||
* This case didn't be place in the default stack parser because the current regex logic is too complex to be maintained. | ||
* Therefore, add this parser here as a `polyfill` is better. | ||
*/ | ||
export const nextStackParser: StackLineParser = [ | ||
highestPriority, | ||
line => { | ||
/** | ||
* Pick the last segment as the filename. | ||
* e.g. | ||
* " at http://localhost:3001/_next/static/chunks/a…page/(default)/sub/page-3d428c1ba734e10f.js:1:126" -> "http://localhost:3001/_next/static/chunks/a…page/(default)/sub/page-3d428c1ba734e10f.js:1:126" | ||
* */ | ||
const waitForReplaceSegments = line.split(' '); | ||
|
||
// Keep the last segment as the filename and use it to replace the original filename at the end | ||
let waitForReplaceFilename = waitForReplaceSegments[waitForReplaceSegments.length - 1]; | ||
|
||
// In this case, the filename is a URL. So we need to check if it is a valid URL. | ||
const fileUrlRegex = /^(\(?)(https?:\/\/[^/]+(\/(?:[^/[\]]+|\[\[?\.{0,3}locale\]?\])*)*)(\)?)$/; | ||
|
||
if (!fileUrlRegex.test(waitForReplaceFilename) || !waitForReplaceFilename) { | ||
return defaultStackParser(line)[0]; | ||
} | ||
|
||
/** | ||
* Remove parentheses. | ||
* e.g. | ||
* " (http://localhost:3001/_next/static/chunks/a…page/(default)/sub/page-3d428c1ba734e10f.js:1:126)" -> "http://localhost:3001/_next/static/chunks/a…page/(default)/sub/page-3d428c1ba734e10f.js:1:126" | ||
*/ | ||
waitForReplaceFilename = waitForReplaceFilename.replace(/^\s*\(|\)\s*$/g, ''); | ||
|
||
const processedLine = line.replace( | ||
waitForReplaceFilename, | ||
/** | ||
* KEY PARTS: Replace special characters with their ASCII code | ||
* " at http://localhost:3001/_next/static/chunks/app/[locale]/sentry-example-page/(default)/sub/page-3d428c1ba734e10f.js:1:126" -> | ||
* " at http://localhost:3001/_next/static/chunks/app/%5Blocale%5D/sentry-example-page/(default)/sub/page-3d428c1ba734e10f.js:1:126" | ||
* This is used to avoid the URL being split from the `(default)` part. | ||
*/ | ||
waitForReplaceFilename.replace(/[()@[\]]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`), | ||
); | ||
|
||
return defaultStackParser(processedLine)[0]; | ||
}, | ||
]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.