Skip to content

Conversation

@dariatiurina
Copy link
Contributor

Fixed the ErrorBoundary multiple errors bug

Description

This pull request enhances error handling in Blazor components, especially around error boundaries and faulted tasks during component initialization and rendering. It introduces new tests and supporting components to ensure that exceptions thrown in lifecycle methods or during rendering are correctly captured by error boundaries, and that error boundaries can handle multiple simultaneous errors.

Changes:

  • Modified ComponentBase to avoid calling StateHasChanged when the initialization task is faulted, preventing unnecessary state updates after errors.
  • Added new test in ComponentBaseTest.cs to verify that error boundary capture exceptions from faulted tasks in OnInitializedAsync and during rendering, and that these errors are surfaced correctly.
  • Introduced TestErrorBoundary and TestComponentErrorBuildRenderTree test components to simulate and verify error boundary behavior with faulted tasks and rendering errors.

Fixes #39814

@dariatiurina dariatiurina self-assigned this Aug 14, 2025
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Aug 14, 2025
@dariatiurina dariatiurina marked this pull request as ready for review August 14, 2025 10:29
Copilot AI review requested due to automatic review settings August 14, 2025 10:30
@dariatiurina dariatiurina requested a review from a team as a code owner August 14, 2025 10:30
Copy link
Contributor

Copilot AI left a comment

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 fixes a bug in Blazor's ErrorBoundary handling where multiple simultaneous errors could cause issues. The fix prevents unnecessary state updates when component initialization tasks are faulted, improving error boundary behavior.

Key changes:

  • Modified ComponentBase to skip StateHasChanged() calls when initialization tasks are faulted
  • Added comprehensive test coverage for error boundary scenarios with multiple errors
  • Created test components to simulate and verify error boundary behavior with faulted tasks

Reviewed Changes

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

Show a summary per file
File Description
src/Components/Components/src/ComponentBase.cs Added condition to prevent StateHasChanged when initialization task is faulted
src/Components/Components/test/ComponentBaseTest.cs Added unit test and helper components for error boundary testing
src/Components/test/testassets/BasicTestApp/ErrorBoundaryTest/MultipleErrorsChild.razor New test component that throws errors in both OnInitializedAsync and BuildRenderTree
src/Components/test/testassets/BasicTestApp/ErrorBoundaryTest/ErrorBoundaryCases.razor Added UI test case for multiple errors scenario
src/Components/test/E2ETest/Tests/ErrorBoundaryTest.cs Added E2E test for multiple errors handling

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

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

Looks great, minor comments about a case we missed and a couple extra tests to capture the new behavior.

Good job!

@dariatiurina dariatiurina merged commit 5e64726 into dotnet:main Aug 15, 2025
29 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 15, 2025
@dariatiurina dariatiurina deleted the 39814-fix-errorboundary branch August 26, 2025 10:45
@ScarletKuro
Copy link

ScarletKuro commented Nov 12, 2025

This probably should be marked as breaking change, see MudBlazor/MudBlazor#12098 (comment)
This change affects userspace, since some frameworks depended on the previous behavior, which effectively made it a feature rather than a bug. This change should be documented in https://learn.microsoft.com/en-us/dotnet/core/compatibility/10.0 if there are no plans to reverse it.

@javiercn
Copy link
Member

@ScarletKuro please file a new issue with the problem. We won't effectively track comments on closed PRs. Please also detail the concrete use case in which this change represents a problem with MudBlazor

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Blazor doesn't render ErrorBoundary's ErrorContent when there are two different exceptions inside it's ChildContent

3 participants