Skip to content

Improve Render Output Invalid Exception Message to Clarify Common Control Flow Causes - #67695

Open
Yuvan111 wants to merge 15 commits into
dotnet:mainfrom
Yuvan111:fix-55374-invalid-msg
Open

Yuvan111 wants to merge 15 commits into
dotnet:mainfrom
Yuvan111:fix-55374-invalid-msg

Conversation

@Yuvan111

@Yuvan111 Yuvan111 commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Improve Render Output Invalid Exception Message to Clarify Common Control Flow Causes

Description

This change improves the Render output is invalid exception message emitted by RenderTreeBuilder when a render frame is left unclosed. The previous message only referenced try/catch blocks, which can make diagnosis difficult when the root cause is another form of control-flow interruption. The updated message clarifies that the exception occurs whenever rendering exits before a matching CloseElement, CloseComponent, or CloseRegion call is reached, including common scenarios such as break, continue, return, throw, goto, or early exits from try/catch blocks. Additionally, a link to the Blazor advanced scenarios documentation is included to help developers more quickly identify and resolve invalid render tree construction issues.

Before

image

After

Screenshot 2026-08-27 at 12 57 38 PM

Fixes #55374

@Yuvan111
Yuvan111 requested a review from a team as a code owner July 9, 2026 09:15
Copilot AI review requested due to automatic review settings August 7, 2026 03:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request updates Blazor’s RenderTreeBuilder invalid render-tree exception message to better explain that unclosed frames typically come from control-flow exiting rendering before the corresponding Close* call is reached, and adds/updates tests to validate the new messaging.

Changes:

  • Expanded InvalidOperationException text in RenderTreeBuilder.AssertTreeIsValid to cover common control-flow causes (e.g., break, return, throw) and added a docs link.
  • Updated existing tests to avoid asserting the full legacy message and to validate key new message phrases.
  • Added a new test intended to cover a break scenario.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Components/Components/src/Rendering/RenderTreeBuilder.cs Updates the thrown exception message when frames are left unclosed.
src/Components/Components/test/Rendering/RenderTreeBuilderTest.cs Updates/extends unit tests to validate the new exception message text and adds a new break-focused test.
src/Components/Components/test/RendererTest.cs Updates renderer-level test assertions to match the revised message content.

@PureWeen

Copy link
Copy Markdown
Member

Review result: I do not think the diagnostic and regression test are ready to merge at the current head (47583273ae).

AssertTreeIsValid runs only after the render fragment returns normally. If a render-fragment exception escapes, ComponentState.RenderIntoBatch catches it and returns before validation, so an uncaught throw cannot produce this diagnostic.

The message should instead describe the actual invariant: every opened element, component, or region must reach its matching CloseElement, CloseComponent, or CloseRegion. Control flow can cause this error when it skips a close but still lets the fragment complete normally—for example, break, continue, return, goto, or a caught exception that permits completion. The current “exited the render method” wording is misleading for break and continue and overstates the uncaught-throw case.

The regression is also not causal as written. AddContent(RenderFragment) inserts a Region, so the assertion reports an unclosed Region rather than demonstrating that break left the intended element open. Please construct the invalid sequence directly with a RenderTreeBuilder, assert that the frame is the unclosed Element, and verify the corrected complete message.

I validated this against the exact head: the identical direct assertion fails with the current implementation and passes with the corrected diagnostic and regression test. The focused Components tests then pass 125/125.

This review was generated with GitHub Copilot under human direction and validated by a human.

@Yuvan111

Copy link
Copy Markdown
Contributor Author

@PureWeen, Thanks for the review. I’ve addressed the diagnostic wording and regression test feedback. All 125 focused tests pass.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Sep 26, 2026

This branch has not been deployed

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

Labels

area-blazor Includes: Blazor, Razor Components pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Render Output Invalid: Improve exception message

4 participants