Skip to content

[SDK-801] Document reporting errors from Next.js proxy/middleware (#125) - #186

Open
devtools-agent[bot] wants to merge 1 commit into
mainfrom
sdk-801/nextjs-proxy-server-token
Open

devtools-agent[bot] wants to merge 1 commit into
mainfrom
sdk-801/nextjs-proxy-server-token

Conversation

@devtools-agent

Copy link
Copy Markdown

Linear: SDK-801 · Closes #125

What changed

  • examples/nextjs-approuter/src/proxy.ts (new): reports errors with the shared serverInstance. A matcher limits it to a /proxy_error demo route, which throws, reports to Rollbar and returns a 500. Request data goes in per call (rollbar.error(err, { pathname })) and not through rollbar.configure(), because configure() would change the instance every concurrent request shares. That was the pattern in the original issue.
  • examples/nextjs-approuter/README.md: new section Reporting errors from proxy.ts (formerly middleware.ts) that explains the Edge-runtime 403 and gives three options: rename to proxy.ts on Next 16, use runtime: 'nodejs' on Next 15.5 and later, or create a separate new Rollbar(clientConfig) instance if the middleware has to stay on Edge. The env var names in the README snippet now match src/rollbar.ts (NEXT_PUBLIC_ROLLBAR_CLIENT_TOKEN / ROLLBAR_SERVER_TOKEN); before, the snippet used different names than the code.
  • README.md: one paragraph under Using with Next.js App Router that links to the new section.
  • src/__tests__/proxy.test.tsx (new): checks that the proxy reports through serverInstance with per-call data, never calls configure(), returns a 500 on the demo route and passes other requests through.

Why

#125 is a docs/example gap, not an SDK bug. On the Edge runtime the bundler resolves rollbar through the browser export condition, so middleware gets the browser build. That build sends platform: browser items, and the API rejects them when they come with a post_server_item token.

Validation

Run against Next 16.3.8 and rollbar 3.1.0, with endpoint temporarily pointed at a local listener so the payloads could be inspected:

Setup Bundled build Item sent with server token
middleware.ts (Edge, default) rollbar-browser-js platform: browser → 403 on the real API (reproduces #125)
middleware.ts + runtime: 'nodejs' node_rollbar server item, accepted
proxy.ts (this PR) node_rollbar server item, accepted
  • next build and next start with the new proxy.ts: GET /proxy_error returned 500 and sent one node_rollbar item; GET / returned 200 and sent nothing.
  • jest --ci: 4 suites and 5 tests pass. eslint . --max-warnings 0 and tsc --noEmit pass, and prettier passes on the changed files.

🤖 Generated with Claude Code

Issue #125: calling the server instance from Edge middleware fails with
"post_client_item scope is required" because on the Edge runtime the
bundler resolves `rollbar` to its browser build, which sends browser
items that a post_server_item token cannot post.

- Add src/proxy.ts to the nextjs-approuter example. Next.js 16 runs
  proxy.ts on the Node.js runtime, so it gets the server build and the
  server token works. Request data is passed per call instead of via
  rollbar.configure(), which would mutate the shared instance.
- Document the proxy pattern and the Edge middleware options
  (proxy.ts, runtime: 'nodejs', or a client-token instance).
- Fix the example README's env var names to match src/rollbar.ts.

Closes #125

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@linear-code

linear-code Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

SDK-801

CR-577

@devtools-agent

Copy link
Copy Markdown
Author

AI Agent Review (openai, openai-astra)

Review: document reporting from Next.js proxy.ts / middleware

I found nothing in the changed lines that needs fixing. I read the new and changed files against the example's code and the pinned dependencies.

What I checked

  • examples/nextjs-approuter/src/proxy.ts:10-30 uses the shared serverInstance and passes request data per call (rollbar.error(error as Error, { pathname })). That call matches rollbar 3.1.0's types: error(...args: LogArgument[]) takes an Error and a plain object (node_modules/rollbar/index.d.ts:20,165-173). The code never calls configure(). Next.js is pinned to 16.3.8 (examples/nextjs-approuter/package.json:15, examples/nextjs-approuter/package-lock.json:7895), and proxy.ts is the Next 16 convention.
  • The PR says the Edge runtime gets rollbar's browser build. That fits rollbar 3.1.0's export map, which has separate node and browser entries (node_modules/rollbar/package.json:22-33).
  • The env var names in the example README snippet now match the real code (examples/nextjs-approuter/src/rollbar.ts:10,15 vs examples/nextjs-approuter/README.md:34,39).
  • The test's jest.mock('../rollbar') should also cover the proxy's @/rollbar import, because both resolve to the same file. Another test in the same suite already imports through @/ (examples/nextjs-approuter/src/__tests__/next-error-handler.test.tsx:4). The test file uses .tsx because testMatch only picks up *.test.tsx (examples/nextjs-approuter/jest.config.js:9). Its @jest-environment node docblock replaces the default jsdom environment for this file only.
  • As far as I can check by hand, the new code follows the repo's prettier config (prettier.config.js:4-7: single quotes, trailing commas, default width). CI runs it on examples/, which is not in .prettierignore.
  • The root README link anchor #reporting-errors-from-proxyts-formerly-middlewarets matches the GitHub slug of the new heading (examples/nextjs-approuter/README.md:70).
  • The example tests run in CI through npm run test:examples (.github/workflows/ci.yaml:76-77). I could not run them, so I am not claiming they pass.

Existing issues in unchanged lines (not findings)

  • The root README.md snippet still uses the old env var names NEXT_PUBLIC_POST_CLIENT_ITEM_TOKEN / POST_SERVER_ITEM_TOKEN (README.md:156,161). This PR fixed the same mismatch in the example README, so the two READMEs now disagree. Worth updating while you're here.
  • examples/nextjs-approuter/README.md:60 has an empty link [error handler]() and a double slash in ./src/app//next_error_handler/error.tsx.

Optional suggestions

  • Edge option (examples/nextjs-approuter/README.md:105): if a reader imports clientConfig from @/rollbar into Edge middleware, the import also runs new Rollbar(...) with the server token (examples/nextjs-approuter/src/rollbar.ts:14-17). Edge would load the browser build, and the config has captureUncaught/captureUnhandledRejections turned on (examples/nextjs-approuter/src/rollbar.ts:3-7). The browser build attaches its global handlers to self when there is no window (node_modules/rollbar/src/browser/core.js:208-231,581-585). I did not check whether Edge ever fires those handlers. It may still be worth saying to define the Edge instance in an Edge-only module.
  • The proxy returns without waiting for the report to send. That works with next start, which is what the PR says it validated. If the docs are meant to cover serverless hosts, they could mention rollbar's wait() flush API (node_modules/rollbar/index.d.ts:22).

The PR description declares no dependencies on other PRs; it only references issue #125 and SDK-801.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Next.js AppRoute Example: How can we log an error in the middleware

1 participant