onRequestError function from your instrumentation.js (or .ts) file, you can forward these errors to Atatus with full request and routing context.
Why This Is Required
Next.js catches errors thrown during Server Component rendering, Route Handlers, and Server Actions internally, and serves a generic error page. Because the framework handles these errors before they propagate, the Atatus agent cannot capture them through standard auto-instrumentation alone. TheonRequestError hook provides a global entry point to intercept and report these errors.
Setup
Create aninstrumentation.js (or instrumentation.ts) file in your project root (or inside src/ if your project uses the src layout).
If you already have an
instrumentation.js file, add the onRequestError export to it alongside your existing code.JavaScript
TypeScript
How It Works
When an unhandled error occurs during server-side rendering or routing, Next.js invokesonRequestError. The function:
- Guards against non-Node.js runtimes (Edge Runtime is not supported).
- Dynamically requires
atatus-nodejsto avoid bundler conflicts. - Reports the error via
atatus.notifyErrorwith request metadata and the Next.js error digest.
Troubleshooting
If unhandled server-side errors are not appearing after setup:- Incorrect file location: Place
instrumentation.js/instrumentation.tsin the project root or insidesrc/(adjacent toapporpages). It must not be placed insideapp/orpages/. - Missing export: The function must be exported:
export async function onRequestError. - Server not restarted: Next.js compiles the instrumentation file at startup. You must restart the dev or production server after any changes.
- Next.js 14, experimental flag required: Enable the instrumentation hook explicitly in
next.config.js:
Exclude Packages from Bundling
Next.js bundles server-side dependencies by default. The Atatus agent instruments packages by intercepting Node.jsrequire() calls at runtime, and this only works when packages are loaded natively, not compiled into the bundle.
Add atatus-nodejs and any database drivers you use to serverExternalPackages in next.config.js:
require() at runtime, enabling the Atatus agent to instrument database queries, external HTTP calls, and other operations.