@unhandled/nextjs
v0.2.18
Published
Unhandled for Next.js: instrumentation.ts registration, request error reporting, route handler and server action wrappers on @unhandled/node
Readme
@unhandled/nextjs
Sends traces, logs, metrics and server errors from a Next.js 15+ App Router app to Unhandled. It starts @unhandled/node from instrumentation.ts and reports errors through Next's onRequestError hook. Next's own request spans become the SERVER spans Unhandled groups by route.
Setup
Set UNHANDLED_DSN (from the service settings page) in the server environment, then add three things.
instrumentation.ts at the project root, or in src/ when you use one:
export { register, onRequestError } from '@unhandled/nextjs'register reads its options from the environment. To pass options, call registerUnhandled from your own register:
import { registerUnhandled } from '@unhandled/nextjs'
export const register = () => {
registerUnhandled({ serviceName: 'storefront' })
}
export { onRequestError } from '@unhandled/nextjs'next.config.ts:
import { withUnhandledConfig } from '@unhandled/nextjs'
export default withUnhandledConfig({
// your config
})withUnhandledConfig adds @unhandled/nextjs and @unhandled/node to serverExternalPackages and keeps any entries you already have. Without it Next bundles a separate copy of the SDK into instrumentation.ts and into every route. The copies do not share state, so setUser cannot reach the request span and a server action error is reported twice. If you prefer to write the setting yourself, it is serverExternalPackages: ['@unhandled/nextjs', '@unhandled/node'].
registerUnhandled(options?) takes every @unhandled/node option (dsn, environment, serviceName, samplingRatio, http, instrumentations, integrations and the rest) and returns the running client or null. It only starts the SDK when NEXT_RUNTIME is nodejs. On the edge runtime the package resolves to a build where register, registerUnhandled, onRequestError, setUser and withServerAction do nothing, so Edge routes and the edge compile of instrumentation.ts load no Node.js code and send nothing.
Like @unhandled/node, it redacts secret query values and secret path segments (not ids) from URL attributes by default (http.sanitizeUrls: false turns that off), and reads source lines only from .js/.ts source files under the app root, never from paths named in an error message or from dotfiles such as .env. The @unhandled/node README has the rules.
What you get
Next opens a SERVER span for every request, named GET /api/users/[id], with http.route set to the route pattern (/api/users/[id], /checkout) and next.route, http.method, http.target and http.status_code next to it. Unhandled uses that span as the request.
onRequestError reports errors thrown by route handlers, Server Components, server actions and the proxy with mechanism nextjs.on_request_error and handled: false. Each report carries these tags:
| Tag | Value |
| --- | --- |
| nextjs.route_path | the route, for example /api/boom |
| nextjs.route_type | route, render, action or proxy |
| nextjs.router_kind | App Router or Pages Router |
| nextjs.render_source | when rendering, for example react-server-components |
| nextjs.revalidate_reason | on-demand or stale during revalidation |
| nextjs.digest | the digest Next shows in place of the message in production |
notFound(), redirect(), permanentRedirect(), forbidden(), unauthorized() and dynamic-rendering bailouts are control flow, not errors, and are never reported. The ignoreExceptions, ignoreStatuses and ignoreCodes options apply as well.
Users
import { setUser } from '@unhandled/nextjs'
export const GET = async (request: Request) => {
const user = await currentUser(request)
setUser({ id: user.id, email: user.email })
return Response.json(user)
}setUser writes user.id, user.email and user.name on the active span and on the request's SERVER span.
Server actions
Next names a server action request after the page that posts it (POST /checkout), so two actions on one page look the same. Wrap an action to give it a span of its own:
'use server'
import { withServerAction } from '@unhandled/nextjs'
export const submitOrder = withServerAction('submitOrder', async (form: FormData) => {
// ...
})The action runs inside a server action submitOrder span with the attribute nextjs.server_action. If it throws, the error is reported once with mechanism nextjs.server_action and the tag nextjs.server_action, then rethrown, so Next still renders its error. A redirect() or notFound() from the action passes through and does not mark the span as failed.
Route handlers need no wrapper, because Next already names their spans by route and onRequestError already reports their errors.
Everything else
Unhandled.track, captureException, captureMessage, record and the decorators come from @unhandled/node, which is installed with this package. Import them from there.
For AI agents
A coding agent can install this package, check that its data arrives and fix the errors it reports through the Unhandled MCP server at https://unhandled.net/mcp. In Claude Code, the Unhandled plugin adds that server plus skills for instrumenting a service, fixing a production error with a failing test first and verifying a deploy:
claude plugin marketplace add treboryx/unhandled
claude plugin install unhandled@unhandledCodex, Cursor, Gemini CLI and other agents connect to the same URL. Coding agents has the setup for each one and an AGENTS.md section to add to your repository.
