Skip to content

Commit 6e9682a

Browse files
authored
Merge pull request #15488 from getsentry/warn-missing-request-error-handler-2
fix(nextjs/instrumentation): warn about missing onRequestError handler
2 parents 7e18a44 + a37b66d commit 6e9682a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

Diff for: packages/nextjs/src/config/webpack.ts

+54
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export function constructWebpackConfigFunction(
5757
if (runtime !== 'client') {
5858
warnAboutDeprecatedConfigFiles(projectDir, runtime);
5959
}
60+
if (runtime === 'server') {
61+
warnAboutMissingonRequestErrorHandler(projectDir);
62+
}
6063

6164
let rawNewConfig = { ...incomingConfig };
6265

@@ -435,6 +438,57 @@ async function addSentryToClientEntryProperty(
435438
return newEntryProperty;
436439
}
437440

441+
/**
442+
* Make sure the instrumentation file has a `onRequestError` Handler
443+
*
444+
* @param projectDir The root directory of the project, where config files would be located
445+
*/
446+
function warnAboutMissingonRequestErrorHandler(projectDir: string): void {
447+
const instrumentationPaths = [
448+
['src', 'instrumentation.ts'],
449+
['src', 'instrumentation.js'],
450+
['instrumentation.ts'],
451+
['instrumentation.js'],
452+
];
453+
const instrumentationFile = instrumentationPaths
454+
.map(pathSegments => path.resolve(projectDir, ...pathSegments))
455+
.find(function exists(filePath: string): string | null {
456+
try {
457+
fs.accessSync(filePath, fs.constants.F_OK);
458+
return filePath;
459+
} catch (error) {
460+
return null;
461+
}
462+
});
463+
464+
function hasOnRequestErrorHandler(absolutePath: string): boolean {
465+
try {
466+
const content = fs.readFileSync(absolutePath, 'utf8');
467+
return content.includes('onRequestError');
468+
} catch (error) {
469+
return false;
470+
}
471+
}
472+
473+
if (!instrumentationFile) {
474+
// eslint-disable-next-line no-console
475+
return console.warn(
476+
`${chalk.yellow(
477+
'[@sentry/nextjs]',
478+
)} 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`,
479+
);
480+
}
481+
482+
if (!hasOnRequestErrorHandler(instrumentationFile)) {
483+
// eslint-disable-next-line no-console
484+
console.warn(
485+
`${chalk.yellow(
486+
'[@sentry/nextjs]',
487+
)} 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`,
488+
);
489+
}
490+
}
491+
438492
/**
439493
* Searches for old `sentry.(server|edge).config.ts` files and Next.js instrumentation hooks and warns if there are "old"
440494
* config files and no signs of them inside the instrumentation hook.

0 commit comments

Comments
 (0)