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 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 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
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
2 participants