Skip to content

Commit 572a5d7

Browse files
committed
warn about missing instrumentation file
and only warn about missing onRequestError when an instrumentation file exists
1 parent df67430 commit 572a5d7

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

packages/nextjs/src/config/webpack.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,18 +448,42 @@ function warnAboutMissingonRequestErrorHandler(projectDir: string): void {
448448
['instrumentation.ts'],
449449
['instrumentation.js'],
450450
];
451-
function hasOnRequestErrorHandler(pathSegments: string[]): boolean {
452-
const filePath = path.resolve(projectDir, ...pathSegments);
451+
const instrumentationFile = instrumentationPaths
452+
.map(pathSegments => path.resolve(projectDir, ...pathSegments))
453+
.find(function exists(filePath: string): string | null {
454+
try {
455+
fs.accessSync(filePath, fs.constants.F_OK);
456+
return filePath;
457+
} catch (error) {
458+
return null;
459+
}
460+
});
461+
462+
function hasOnRequestErrorHandler(absolutePath: string): boolean {
453463
try {
454-
const content = fs.readFileSync(filePath, 'utf8');
464+
const content = fs.readFileSync(absolutePath, 'utf8');
455465
return content.includes('export const onRequestError');
456466
} catch (error) {
457467
return false;
458468
}
459469
}
460-
if (!instrumentationPaths.some(hasOnRequestErrorHandler)) {
470+
471+
if (!instrumentationFile) {
472+
// eslint-disable-next-line no-console
473+
return console.warn(
474+
`${chalk.yellow(
475+
'[@sentry/nextjs]',
476+
)} Could not find instrumentation file: https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-initialization-config-files`,
477+
);
478+
}
479+
480+
if (!hasOnRequestErrorHandler(instrumentationFile)) {
461481
// eslint-disable-next-line no-console
462-
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`);
482+
console.warn(
483+
`${chalk.yellow(
484+
'[@sentry/nextjs]',
485+
)} 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`,
486+
);
463487
}
464488
}
465489

0 commit comments

Comments
 (0)