Skip to content
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

fix(nextjs/instrumentation): warn about missing onRequestError handler #15488

Merged
merged 5 commits into from
Feb 25, 2025
Merged
Changes from all 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
54 changes: 54 additions & 0 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export function constructWebpackConfigFunction(
if (runtime !== 'client') {
warnAboutDeprecatedConfigFiles(projectDir, runtime);
}
if (runtime === 'server') {
warnAboutMissingonRequestErrorHandler(projectDir);
Copy link
Member

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'.

Copy link
Collaborator Author

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 now

Copy link
Member

Choose a reason for hiding this comment

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

oh right. Yeah perfect

}

let rawNewConfig = { ...incomingConfig };

Expand Down Expand Up @@ -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 {
const instrumentationPaths = [
['src', 'instrumentation.ts'],
['src', 'instrumentation.js'],
['instrumentation.ts'],
['instrumentation.js'],
];
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.
Expand Down
Loading