nextjs - ✅(Solved) Fix Next.js 16 does not forward proxy errors to `onRequestError` [2 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#85261Fetched 2026-04-08 02:16:43
View on GitHub
Comments
2
Participants
2
Timeline
7
Reactions
3
Author
Participants
Timeline (top)
commented ×2cross-referenced ×2issue_type_added ×1labeled ×1

Error Message

I would expect the error reporting to behave identically between these two files.

Fix Action

Fixed

PR fix notes

PR #17995: feat(nextjs): Support node runtime on proxy files

Description (problem / solution / changelog)

Next 16 was released

With that proxy files run per default on nodejs.

This PR

  • Updates the tests to run on next 16 (non-beta)
  • Adds support for handling middleware transactions in the node part of the sdk

closes https://github.com/getsentry/sentry-javascript/issues/18006 closes https://github.com/getsentry/sentry-javascript/issues/18005 closes https://github.com/getsentry/sentry-javascript/issues/18008

Changed files

  • dev-packages/e2e-tests/test-applications/nextjs-16/package.json (modified, +12/-1)
  • dev-packages/e2e-tests/test-applications/nextjs-16/tests/middleware.test.ts (modified, +39/-26)
  • packages/nextjs/src/common/nextSpanAttributes.ts (added, +3/-0)
  • packages/nextjs/src/server/index.ts (modified, +30/-9)

PR #91523: Fix: Server Actions do not await onRequestError from instrumentation

Description (problem / solution / changelog)

Bug Fix: Await onRequestError promise for server action errors

Disclaimer: This is my first time in the Next.js codebase. I used AI tooling (Claude) to help trace the error flow and generate the fix + test. I've done my best to verify the analysis, but I may have misread the code or missed context that's obvious to maintainers. Please take the specifics with a grain of salt — I'd rather flag that upfront than waste your time reviewing something built on a wrong assumption. Happy to adjust or close if the approach is off.

What?

Register the Promise returned by onRequestError (from instrumentation.ts) with after() when the error originates from a server action, so async error reporting completes before serverless runtimes tear down.

Why?

As far as I can tell, createReactServerErrorHandler in create-error-handler.tsx calls onReactServerRenderError(err, silenceLog) without awaiting the returned Promise. The Promise appears to be silently discarded, which would turn async error reporting into a race condition.

If this reading is correct, it would affect error tracking integrations that do async work inside onRequestError — Sentry, PostHog, Datadog, custom solutions, etc. The hook fires, but the async work (HTTP requests to ingest APIs) may not complete before the response lifecycle ends.

  • Affected (I think): Server action errors (App Router)
  • Not affected: Route handler errors (these appear to be properly awaited in app-route.ts)
  • Severity: In serverless environments (Vercel Functions, AWS Lambda), the runtime may freeze or terminate after the response is sent. On long-running servers it would be a race condition but would usually complete in practice.

Error flow as I understand it: Server action throws → error wrapped in Promise.reject() → embedded in RSC payload → React's renderToReadableStream calls synchronous onError callback → onReactServerRenderError returns a Promise → Promise discarded (line 144).

Route handlers use a separate code path (app-route.ts:528) that properly awaits the same instrumentation chain.

How?

React's onError callback must be synchronous (returns a digest string), so we can't await inline. The fix uses Next.js's existing after() API to register the instrumentation promise with the platform's waitUntil mechanism. The after() call itself is synchronous — it just registers the promise — so the error handler still returns err.digest immediately.

const instrumentationPromise = onReactServerRenderError(err, silenceLog)
if (instrumentationPromise) {
  try {
    const { after } = require('../after/after')
    after(instrumentationPromise)
  } catch {
    // after() unavailable (e.g. build-time prerendering) — fall back to fire-and-forget
  }
}

The try/catch handles cases where AfterContext is unavailable (build-time prerendering, outside request scope).

Note: createHTMLErrorHandler in the same file has what looks like the same pattern — onHTMLRenderSSRError also returns a Promise that is discarded. I didn't touch it since I couldn't confirm with a concrete reproduction.

Test plan

  • e2e test added at test/e2e/on-request-error/server-action-error-async/
  • Test injects a mock waitUntil via @next/request-context (same pattern as test/e2e/app-dir/next-after-app)
  • Test verifies waitUntil is called (proving after() registered the promise)
  • Test verifies the error was actually reported (fetch to /write-log completed)
  • Test verifies correct error metadata (route path, method, routerKind, routeType, renderSource)

Checklist

  • Related issues linked
  • Tests added
  • No new errors requiring helpful links

Possibly related issues: #83404, #85261 Related: Sentry discussion #11709

Changed files

  • packages/next/src/server/app-render/create-error-handler.tsx (modified, +20/-2)
  • test/e2e/on-request-error/server-action-error-async/app/form-error/page.js (added, +17/-0)
  • test/e2e/on-request-error/server-action-error-async/app/layout.js (added, +10/-0)
  • test/e2e/on-request-error/server-action-error-async/app/write-log/route.js (added, +40/-0)
  • test/e2e/on-request-error/server-action-error-async/instrumentation.js (added, +40/-0)
  • test/e2e/on-request-error/server-action-error-async/next.config.js (added, +1/-0)
  • test/e2e/on-request-error/server-action-error-async/server-action-error-async.test.ts (added, +76/-0)

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.14.0
  npm: 10.7.0
  Yarn: 1.22.22
  pnpm: 9.4.0
Relevant Packages:
  next: 16.0.0 // Latest available version is detected (16.0.0).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/chargome/repro.next-16-proxy-errors

To Reproduce

Reproduction steps are outlined in the repro readme.

Current vs. Expected behavior

  • Throwing errors from within a proxy.ts file do not get reported via onRequestError
  • Throwing errors from within a middleware.ts file do get reported via onRequestError

I would expect the error reporting to behave identically between these two files.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.14.0
  npm: 10.7.0
  Yarn: 1.22.22
  pnpm: 9.4.0
Relevant Packages:
  next: 16.0.0 // Latest available version is detected (16.0.0).
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next start (local), next dev (local), Vercel (Deployed)

Additional context

No response

extent analysis

TL;DR

Review the proxy.ts file to ensure error handling is properly configured to report errors via onRequestError.

Guidance

  • Verify that the proxy.ts file is correctly set up to handle and report errors, checking for any differences in error handling compared to the middleware.ts file.
  • Check the Next.js documentation for any specific requirements or limitations on error reporting in proxy files.
  • Test the error reporting behavior in the proxy.ts file by intentionally throwing an error and checking if it is reported via onRequestError.
  • Compare the implementation of error handling in middleware.ts and proxy.ts to identify any discrepancies that might cause the difference in error reporting behavior.

Example

No code snippet is provided due to lack of specific implementation details in the issue.

Notes

The solution may depend on the specific implementation of the proxy.ts and middleware.ts files, which are not provided in the issue. Additionally, the issue might be related to a specific version of Next.js or other dependencies.

Recommendation

Apply workaround: Review and adjust the error handling in the proxy.ts file to match the expected behavior, as the issue seems to be related to the specific implementation rather than a version-related bug.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING