-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nextjs/instrumentation): warn about missing onRequestError handler #15488
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b6d3367
fix(nextjs/instrumentation): warn about missing onRequestError handler
a-hariti df67430
better warning message
a-hariti 572a5d7
warn about missing instrumentation file
a-hariti 4758a9d
review items
a-hariti a37b66d
better warning message
a-hariti 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,9 @@ export function constructWebpackConfigFunction( | |
if (runtime !== 'client') { | ||
warnAboutDeprecatedConfigFiles(projectDir, runtime); | ||
} | ||
if (runtime === 'server') { | ||
warnAboutMissingonRequestErrorHandler(projectDir); | ||
} | ||
|
||
let rawNewConfig = { ...incomingConfig }; | ||
|
||
|
@@ -435,6 +438,57 @@ async function addSentryToClientEntryProperty( | |
return newEntryProperty; | ||
} | ||
|
||
/** | ||
* Make sure the instrumentation file has a `onRequestError` Handler | ||
* | ||
* @param projectDir The root directory of the project, where config files would be located | ||
*/ | ||
function warnAboutMissingonRequestErrorHandler(projectDir: string): void { | ||
a-hariti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const instrumentationPaths = [ | ||
['src', 'instrumentation.ts'], | ||
['src', 'instrumentation.js'], | ||
['instrumentation.ts'], | ||
['instrumentation.js'], | ||
]; | ||
Comment on lines
+447
to
+452
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this approach does not respect |
||
const instrumentationFile = instrumentationPaths | ||
.map(pathSegments => path.resolve(projectDir, ...pathSegments)) | ||
.find(function exists(filePath: string): string | null { | ||
try { | ||
fs.accessSync(filePath, fs.constants.F_OK); | ||
return filePath; | ||
} catch (error) { | ||
return null; | ||
} | ||
}); | ||
|
||
function hasOnRequestErrorHandler(absolutePath: string): boolean { | ||
try { | ||
const content = fs.readFileSync(absolutePath, 'utf8'); | ||
return content.includes('onRequestError'); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
if (!instrumentationFile) { | ||
// eslint-disable-next-line no-console | ||
return console.warn( | ||
`${chalk.yellow( | ||
'[@sentry/nextjs]', | ||
)} Could not find a Next.js instrumentation file. This indicates an incomplete configuration of the Sentry SDK. An instrumentation file is required for the Sentry SDK to be initialized on the server: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-initialization-config-files`, | ||
); | ||
} | ||
|
||
if (!hasOnRequestErrorHandler(instrumentationFile)) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`${chalk.yellow( | ||
'[@sentry/nextjs]', | ||
)} Could not find \`onRequestError\` hook in instrumentation file. This indicates outdated configuration of the Sentry SDK. Use \`Sentry.captureRequestError\` to instrument the \`onRequestError\` hook: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#errors-from-nested-react-server-components`, | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Searches for old `sentry.(server|edge).config.ts` files and Next.js instrumentation hooks and warns if there are "old" | ||
* config files and no signs of them inside the instrumentation hook. | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do this only for
runtime === 'nodejs'
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the runtime is either:
client | edge | server
I used
server
for nowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right. Yeah perfect