ISSUE DESCRIPTION A self-throwing global error component renders no last-resort UI If…
- User
ISSUE DESCRIPTION
A self-throwing global error component renders no last-resort UI
If the global error component itself fails, return the built-in valid HTML fallback with the expected successful fallback status while preserving response headers and cookies. Local error boundaries and framework navigation/control-flow signals must retain their existing semantics, and the original boundary exception must not be exposed.
Interface
No new public interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /vinext, current file: none) bash-$
Assistant
I'll start by exploring the repository structure to understand the codebase and then investigate the issue about the global error component.
Assistant
This is a Vinext monorepo. Let me explore the structure to find the error-related code, particularly global error components and error boundaries.
Assistant
Let me look at the files related to error handling, particularly
app-fallback-renderer.ts,default-global-error-module.ts, andapp-page-boundary.ts.Assistant
This file shows the
createAppFallbackRendererfunction that creates error boundary rendering functions. The key method isrenderErrorBoundarywhich delegates torenderAppPageErrorBoundary. Let me look at that function to understand how the error boundary is rendered.Assistant
Now let me look at the
app-page-boundary.tsfile to understand the boundary resolution and wrapping logic:Assistant
Let me search for tests related to error boundaries and global error components:
Assistant
Now let me look at the tests for app-router-production-server to understand how error boundaries are tested:
Assistant
Let me search more specifically for error-related tests:
Assistant
Let me search for the Next.js source related to global error and see how the RSC rendering handles errors in the global error boundary:
Assistant
Now let me look at the RSC rendering/streaming code to understand how errors in the global error component itself are handled:
Assistant
Let me look at the full recovery logic:
Assistant
Now let me look at
app-page-render.tswhere the main rendering logic is:Assistant
Now I need to understand the issue better. Let me search for how the RSC render handles errors in the global error component itself:
Assistant
Let me look at the AGENTS.md file which might describe the expected behavior:
Assistant
Let me look at the Next.js source to understand what happens when the global error component itself throws:
Assistant
Let me look for tests that relate to this specific scenario - when the global error component throws:
Assistant
Now let me search for how Next.js handles the case where the global error component itself throws an error:
Assistant
Let me look at how the RSC renderer handles errors and how errors in the global error component itself could be caught:
Assistant
Let me look at the app-page-dispatch to understand the overall rendering flow:
Assistant
Now let me look at how
renderAppPageBoundaryResponseandrenderAppPageHtmlStreamWithRecoveryhandle errors. Let me look more closely at therenderErrorBoundaryResponsepath:Assistant
Let me understand the full flow. The key issue is: when the global error component itself fails (throws during rendering), what should happen? According to the issue, it should:
- Return the built-in valid HTML fallback
- Have an expected successful fallback status (200)
- Preserve response headers and cookies
- Not expose the original boundary exception
Let me check how
renderAppPageBoundaryElementResponseworks - this is what actually renders the error boundary:Assistant
Now let me look at the
renderAppPageBoundaryResponseinapp-page-boundary.tsto see the full picture:Assistant
Now I see the flow. When an error boundary is rendered:
renderAppPageErrorBoundarycreates the element with the error boundary component- It calls
renderAppPageBoundaryElementResponsewhich creates the RSC payload renderAppPageBoundaryResponserenders to a readable stream- If it's an HTML request, it calls
createHtmlResponsewhich does SSR
The issue is that when the global error component itself throws during rendering, there's no fallback mechanism to catch that error and return a built-in HTML fallback. Let me look for how Next.js handles this scenario:
Assistant
Continuing my investigation of the error handling flow. I need to understand how
renderAppPageHtmlResponseworks and where the error could be caught when the global error component itself throws.Assistant
Now I understand the flow better. Let me look at how the boundary response is generated - specifically in
renderAppPageBoundaryResponse:The flow is:
renderAppPageBoundaryResponsecreates an RSC stream viarenderToReadableStream- For HTML requests, it calls
createHtmlResponse(rscStream, options.status) createHtmlResponsecallsrenderAppPageHtmlResponsewhich runs SSR- SSR can throw if the global error component itself throws
The issue is that if the global error component throws, the error propagates all the way up and there's no built-in fallback. Let me check the
renderAppPageBoundaryElementResponsefunction to see if there's any try/catch:Assistant
I was in the middle of investigating the code but didn't provide a tool call. Let me continue exploring the codebase to understand where the error handling happens when the global error component itself throws.
Assistant
Now I understand the flow better. Let me look at the
renderAppPageBoundaryResponsefunction and trace through what happens whenrenderToReadableStreamorcreateHtmlResponsethrows.The issue is:
renderAppPageBoundaryResponsecallsrenderToReadableStreamwhich can throw if the global error component throws- For HTML requests, it calls
createHtmlResponsewhich can also throw - There's no try-catch around these, so if the global error component itself throws, the error propagates up
According to the issue, when the global error component itself fails, we should:
- Return the built-in valid HTML fallback
- Have successful fallback status (200)
- Preserve response headers and cookies
- Not expose the original boundary exception
Let me look at the
renderAppPageBoundaryResponsefunction more carefully to understand where the fix should go.Assistant
Let me look at what happens when the error boundary rendering fails and check if there's already a fallback mechanism:
Assistant
I need to understand what the "built-in valid HTML fallback" should look like when the global error component itself throws. Let me search for how Next.js handles this and look at the default global error structure.
Assistant
I now understand the issue. When the global error component itself throws during rendering, there's no fallback mechanism to catch that error. I need to add a try-catch in the error boundary rendering path to handle this "self-throwing" case and return a simple HTML fallback.
Let me first look at the full
renderAppPageErrorBoundaryfunction to understand the current implementation:Assistant
I need to continue looking at the full
renderAppPageErrorBoundaryfunction and understand where I need to add error handling for when the global error component itself throws.Let me see the rest of this function. <parameter=arguments> {"command": "sed -n '440,480p' /vinext/packages/vinext/src/server/app-page-boundary-render.ts"} </tool_call>
Assistant
I need to provide a tool call. Let me continue viewing the file.
Assistant
Now I understand the issue. When
errorBoundary.isGlobalErroris true, the code setsincludeGlobalErrorBoundary: false, meaning there's no outer error boundary to catch errors thrown by the global error component itself. I need to add error handling for this case.Let me check what imports are available in the boundary render file:
Assistant
Now I understand the issue. When the global error component itself throws during rendering, there's no fallback mechanism to catch that error. I need to add error handling in
renderAppPageBoundaryResponseto catch such errors and return a built-in HTML fallback.Let me first look at how headers and cookies should be preserved:
Assistant
I need to check how cookies are handled in the response building process.
Assistant
Now let me understand the full picture:
- The issue is that when the global error component itself throws, there's no fallback mechanism
- I need to wrap the rendering in
renderAppPageBoundaryResponsewith a try-catch - When an error occurs during global error rendering, I should return a built-in HTML fallback
- The fallback should preserve headers and cookies
Let me look at how to build a simple HTML fallback response that preserves headers.
Assistant
Let me understand the full architecture. The
renderAppPageBoundaryResponsefunction is inapp-page-boundary.ts. This function is called fromrenderAppPageBoundaryElementResponseinapp-page-boundary-render.ts.The issue is that when the global error component itself throws (a "self-throwing" error), there's no mechanism to catch it and return a simple HTML fallback.
Looking at the code:
renderAppPageBoundaryResponsecallsrenderToReadableStreamwhich can throw- For HTML requests, it calls
createHtmlResponsewhich can also throw - Neither is wrapped in try-catch
The fix should be:
- Wrap the RSC rendering and HTML response creation in try-catch
- When an error occurs during global error rendering, return a built-in HTML fallback
- The fallback should be a simple HTML document with an error message
- Preserve response headers and cookies
Let me check how
buildAppPageSpecialErrorResponsehandles cookies and headers to understand the pattern.Assistant
Let me look at how cookies are handled in error responses:
Assistant
The issue is: "A self-throwing global error component renders no last-resort UI". The fix needs to:
- When the global error component itself throws, catch that error
- Return a built-in valid HTML fallback (like what Next.js does)
- The fallback should have status 200 (successful fallback status)
- Preserve response headers and cookies
- Local error boundaries and framework navigation/control-flow signals must retain their existing semantics
- The original boundary exception must not be exposed
Looking at the code,
renderAppPageBoundaryResponseis where the error boundary element is rendered. When the global error component throws, this function will throw, and the error propagates up without any fallback.The fix should wrap the rendering logic in
renderAppPageBoundaryResponsewith a try-catch. When it's a global error render (the case), and an error occurs during rendering, we should return a built-in HTML fallback.Assistant
Assistant
I need to run the command properly with the tool call.
Assistant
Good, the function exists. Let me check its implementation to understand what it does.
Assistant
Now I have all the information I need. Let me implement the fix by modifying
app-page-boundary-render.ts.Assistant
I need to use the correct tool call format. Let me open the file properly.
Assistant
I keep getting errors with the tool calls. Let me try using the bash command to view and edit the file instead.
Assistant
Let me implement the fix. I need to:
- Add imports for
getAndClearPendingCookies,applyEdgeRuntimeHeader, andmergeMiddlewareResponseHeaders - Add a helper function
renderGlobalErrorFallback - Wrap the
renderAppPageBoundaryElementResponsecall in a try-catch whenerrorBoundary.isGlobalErroris true
Let me first add the imports, then add the helper function, and finally modify the
renderAppPageErrorBoundaryfunction.- Add imports for
Assistant
The user seems to be having issues with tool calls. Let me try a different approach - using sed to make the edits directly.
Assistant
Good, the sed command worked. Now I need to:
- Add more imports for
applyEdgeRuntimeHeaderandmergeMiddlewareResponseHeaders - Add the helper function
renderGlobalErrorFallback - Modify the
renderAppPageErrorBoundaryfunction to wrap the call in a try-catch
Let me continue adding the imports.
- Add more imports for
Assistant
Good, now I need to add the imports for
applyEdgeRuntimeHeaderandmergeMiddlewareResponseHeaders. Let me check what's currently imported from those files and add the new imports.Assistant
Now I need to:
- Add imports for
applyEdgeRuntimeHeaderfrom./app-page-response.jsandmergeMiddlewareResponseHeadersfrom./middleware-response-headers.js - Add the helper function
renderGlobalErrorFallback - Modify the
renderAppPageErrorBoundaryfunction to wrap the call in a try-catch whenerrorBoundary.isGlobalErroris true
Let me add the imports first, then the helper function, then modify the function.
- Add imports for
Assistant
I need to continue implementing the fix. Let me check if the imports were added correctly and then proceed to add the helper function and modify the
renderAppPageErrorBoundaryfunction.Assistant
Good, the imports were added. Now I need to add the helper function and modify the
renderAppPageErrorBoundaryfunction. Let me first find whererenderAppPageErrorBoundaryis defined and then add the helper function before it.Assistant
Good, the function is at line 382. Now I need to:
- Add the helper function
renderGlobalErrorFallbackbeforerenderAppPageErrorBoundary - Modify the
renderAppPageErrorBoundaryfunction to wrap therenderAppPageBoundaryElementResponsecall in a try-catch
Let me first look at the current implementation of
renderAppPageErrorBoundaryto understand what I need to modify.- Add the helper function
Assistant
Now I understand the structure. I need to:
- Add a helper function
renderGlobalErrorFallbackbeforerenderAppPageErrorBoundary - Wrap the
renderAppPageBoundaryElementResponsecall in a try-catch whenerrorBoundary.isGlobalErroris true
Let me first add the helper function:
- Add a helper function
Assistant
Let me verify the helper function was added correctly and then modify the
renderAppPageErrorBoundaryfunction.Assistant
I need to continue modifying the
renderAppPageErrorBoundaryfunction to wrap therenderAppPageBoundaryElementResponsecall in a try-catch whenerrorBoundary.isGlobalErroris true.Let me find the line where
renderAppPageBoundaryElementResponseis called and wrap it in a try-catch.Assistant
There are two calls to
renderAppPageBoundaryElementResponse. Let me check which one is inrenderAppPageErrorBoundary.Looking at the previous output,
renderAppPageErrorBoundarystarts at line 416 (now after my additions). Let me find the exact line number.Assistant
I need to modify the
renderAppPageErrorBoundaryfunction to wrap therenderAppPageBoundaryElementResponsecall in a try-catch whenerrorBoundary.isGlobalErroris true. The current code is at line 482.The current code:
return renderAppPageBoundaryElementResponse({ ...options, element, initialDevServerError: rawError, layoutModules, navigationParams: matchedParams, route: options.route, routePattern: options.route?.pattern, status: 200, });I need to replace this with:
// When rendering the global-error boundary itself, guard against a // self-throwing component (e.g. a custom global-error.tsx that crashes). // In that case, fall back to a minimal built-in HTML document so the // user always receives a response. Local boundaries and RSC requests // keep their existing semantics. try { return await renderAppPageBoundaryElementResponse({ ...options, element, initialDevServerError: rawError, layoutModules, navigationParams: matchedParams, route: options.route, routePattern: options.route?.pattern, status: 200, }); } catch { if (errorBoundary.isGlobalError && !options.isRscRequest) { return renderGlobalErrorFallback({ clearRequestContext: options.clearRequestContext, isEdgeRuntime: options.isEdgeRuntime, middlewareContext: options.middlewareContext, }); } throw new Error("[vinext] Error boundary render failed"); }Assistant
Now let me use sed to replace the return statement with the try-catch block:
Assistant
I need to replace the return statement in
renderAppPageErrorBoundarywith a try-catch block. Let me find the exact lines and replace them.Assistant
Let me check the line numbers more carefully. I need to replace lines 482-492 (the return statement in renderAppPageErrorBoundary) with the try-catch block.
Assistant
I need to replace lines 482-492 with the try-catch block. Let me use sed to do this replacement.
The current code is:
return renderAppPageBoundaryElementResponse({ ...options, element, initialDevServerError: rawError, layoutModules, navigationParams: matchedParams, route: options.route, routePattern: options.route?.pattern, status: 200, });I need to replace it with the try-catch block. Let me use sed to delete lines 482-492 and insert the new code.
Assistant
Now I need to insert the try-catch block at line 482 (where the old return statement was). Let me do that:
Assistant
Let me verify the changes were applied correctly by viewing the modified section of the file.
Assistant
The changes look good. Let me verify the complete implementation by checking the helper function and the imports.
Assistant
The implementation looks complete. Let me verify the code compiles correctly by running the TypeScript compiler or the project's build command.
Assistant
The build command timed out. Let me try a quicker approach - just check if the TypeScript compiles for the specific file I modified, or run the existing tests to verify the changes work.
Assistant
There's a syntax error - a missing closing brace. Let me check the file structure to find the issue.
Assistant
Let me check the function structure to find the missing closing brace.
Assistant
I see the issue - the
renderAppPageErrorBoundaryfunction is missing its closing brace. Let me fix this by adding the missing}after the try-catch block.Assistant
Let me verify the fix is correct by checking the file structure again.
Assistant
I see the problem - the closing brace was added in the wrong place. There's now
}followed by}which is incorrect. Let me fix this properly by viewing more context and fixing the syntax.Assistant
I see the issue now. The structure is wrong. Looking at lines 506-508:
throw boundaryRenderError; } }There should be just one closing brace for the catch block and one for the function, but the indentation is off. Let me fix this by removing the incorrectly placed braces and adding them correctly.
Assistant
Let me verify the fix:
Assistant
I see there's still an issue - there's an extra
}on line 506. Let me fix this:Assistant
Let me verify the code compiles correctly now.
Assistant
The TypeScript errors shown are from other files in the project and are pre-existing (ES module interop issues). There are no errors related to my modified file
app-page-boundary-render.ts. Let me run the existing tests to verify my changes work correctly.Assistant
The test timed out. Let me try using
vpdirectly as mentioned in the AGENTS.md file, which might be faster.Assistant
Let me try a different approach - run the tests with vitest directly or check what test runner is available.
Assistant
vpexists but the timeout command couldn't find it because it's not in the PATH. Let me use the full path.Assistant
Let me check what's available in the node_modules bin directory and how to run tests properly.
Assistant
I need to run the tests using the vp command. Let me try using the full path to vp.
Assistant
The app-page-boundary-render tests all pass. Let me also run other related tests to verify the changes work correctly.
Assistant
The tests pass. Now let me run a few more related tests to make sure I haven't broken anything. Let me also run the error-boundary tests and the app-fallback-renderer tests.
Assistant
The tests are passing. Now let me also run the app-page-stream tests since those deal with the streaming/rendering logic that my changes interact with.
Assistant
Let me run a few more related tests to ensure nothing is broken.
Assistant
All related tests pass. Let me now verify my implementation is complete by reviewing the final changes.
Assistant
The diff looks good. Let me run a broader set of tests to ensure nothing is broken.
Assistant
All tests pass. Let me also run the app-router-production-server tests to make sure the production server works correctly with my changes.
Assistant
All tests pass. Let me also run the RSC streaming tests to verify my changes work correctly with RSC requests (which should NOT trigger the fallback since we only handle HTML requests).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.