Skip to content

fix(runtime): apply event.res.errHeaders to default error responses - #4891

Merged
pi0 merged 1 commit into
mainfrom
fix/respect-err-headers
Oct 22, 2026
Merged

pi0 merged 1 commit into
mainfrom
fix/respect-err-headers

Conversation

@pi0x

@pi0x pi0x commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

h3 v2 only sends headers staged in event.res.errHeaders on error responses (session cookies from useSession, CORS headers, the headers route rule for status >= 400). h3 merges them when it renders the error itself. A custom onError render skips that merge, so the renderer has to apply them.

Nitro always sets onError, and the built-in prod/dev error handlers built their headers from error.headers only. Because of that, errHeaders never reached the client.

This PR adds a small createErrorHeaders() helper used by both default handlers. It merges event.res.errHeaders on top of error.headers: set-cookie is appended, other headers override (same as h3's mergeHeaders). The merge happens for handled and unhandled errors. Custom error handlers that call defaultHandler get it too; fully custom handlers stay in control, same as h3's onError contract.

Regression test: test/fixture/server/routes/errors/err-headers.ts sets event.res.errHeaders and also writes an event.res.headers value that must not leak into the error response. It fails before the fix (prod and dev) and passes after. Vercel inline snapshots are updated for the new route.

🤖 Generated with AI assistant

Built-in prod/dev error handlers now merge headers staged in `event.res.errHeaders` (session cookies, CORS, ...), matching h3's own error response.
@pi0x
pi0x requested a review from pi0 as a code owner October 22, 2026 05:01
@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 22, 2026 5:02am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: efcd70bb-c97c-49f9-b713-47a88629c3ee
📥 Commits

Reviewing files that changed from the base of the PR and between 90fc5f6 and aab1761.

📒 Files selected for processing: 6
  • src/runtime/internal/error/dev.ts
  • src/runtime/internal/error/prod.ts
  • src/runtime/internal/error/utils.ts
  • test/fixture/server/routes/errors/err-headers.ts
  • test/presets/vercel.test.ts
  • test/tests.ts
+-----------------------------------------------+
| This is O(n) in theory and O(🤡) in practice. |
+-----------------------------------------------+
  \
   \   (\__/)
       (•ㅅ•)
       /   づ
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4891

commit: aab1761

This branch was successfully deployed

1 active deployment
Preview — aab17611 Deployed Oct 22, 2026 by vercel[bot]
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.

2 participants