October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Capture Production Errors in Next.js Route Handlers and Server Actions

Use Next.js App Router instrumentation to report server request errors, distinguish expected action failures, provide safe recovery UI, and track client errors separately.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a Next.js App Router application, use the onRequestError hook in a root-level instrumentation.ts file to report server request errors from Route Handlers and Server Actions. Return expected action failures as data, add route-level error boundaries for user recovery, and instrument client-side errors separately. These are complementary layers: a UI boundary does not replace server error reporting.

Which Next.js routes does this cover?

In the App Router, the current term for server endpoints is Route Handlers, rather than the older Pages Router term “API Routes.” Server Actions are also handled as a distinct request context. Next.js instrumentation can identify the router kind, route path, and route type; the route type is route for Route Handlers and action for Server Actions. This setup is for the App Router and should not be assumed to describe a Pages Router application.

As an Amazon Associate I earn from qualifying purchases.

Report server request errors with instrumentation

Create instrumentation.ts at the application root, or under src if that is where the app lives, and export an onRequestError function. Forward the error, request, and context to your observability provider so reports can be associated with the relevant route or action. Follow the current Next.js instrumentation documentation for the function signature and supported runtime details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
export async function onRequestError(error, request, context) {
  await reportError(error, { request, context })
}

reportError above represents your provider integration; it is not a built-in Next.js function. Adapt the call to the provider’s API and its data-scrubbing requirements. If reporting performs asynchronous work, await it. Next.js specifically cautions: “If you’re running any async tasks in onRequestError, make sure they’re awaited.”

Return expected Server Action failures; throw unexpected errors

Keep ordinary, anticipated outcomes—such as invalid form input or a request that cannot be completed—in the action’s returned value. Render those results as form or action state so a user receives useful feedback without treating routine validation as an incident. Reserve thrown errors for unexpected failures that need operational investigation. See the Next.js error-handling guidance for the framework’s distinction between expected errors and uncaught exceptions.

This distinction improves the signal in production reports: a user correcting an input mistake is a normal application flow, while an unanticipated exception is a failure for the error-reporting path to capture.

Add recovery UI without mistaking it for monitoring

Place an error.tsx file at route-segment boundaries where a local recovery experience makes sense. Consider global-error.tsx when root-level fallback coverage is needed. These boundaries let the interface offer recovery instead of leaving a user at a broken screen, but they do not replace onRequestError or guarantee that every server failure is reported. The error.js reference and production error-handling guide explain the boundary behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Instrument client-side errors separately

Use Next.js’s instrumentation-client.ts entry point for client-side error tracking, following the client instrumentation reference. Do not assume a React error boundary catches every client failure: errors in event handlers and asynchronous callbacks generally need their own reporting path. Verify what your chosen client integration captures and add manual reporting where its documented coverage does not include a failure mode you need.

Keep production error details out of the UI

For production Server Component errors, Next.js shows a generic message and a digest rather than exposing sensitive server exception details in the UI. Keep useful diagnostics in server-side logs and use the digest as a correlation aid when investigating a user report. Show safe recovery guidance to users; do not render stack traces, secret-bearing exception messages, or other internal details. The Next.js error boundary reference describes production error handling.

Review Server Action request limits and origin checks

Next.js documents a default maximum Server Action request body size of 1 MB. The limit is configurable; raise it only when the application has a specific requirement and the resource implications have been considered. Server Actions also use same-origin checks, with additional allowed origins configurable for cases that require them. Review the current serverActions configuration reference before changing either setting.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Production setup checklist

  • Server reporting: export onRequestError from root-level instrumentation.ts and forward the error, request, and context to your reporting system.
  • Action semantics: return expected validation and request failures; throw unexpected exceptions.
  • User recovery: place error.tsx and, where useful, global-error.tsx at appropriate boundaries.
  • Client reporting: configure instrumentation-client.ts and account for event-handler and asynchronous errors that boundaries do not catch.
  • Safe diagnostics: retain server-side logs, correlate with the production digest, and keep internal details out of user-facing output.
  • Request controls: confirm whether the default 1 MB Server Action body limit and same-origin checks fit the application before changing configuration.

The precise provider setup depends on the service and runtime you choose. Confirm its current Next.js support, client and server coverage, source-map and release process, data controls, and any manual instrumentation it requires; the framework hook itself does not establish that a provider automatically captures every failure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.