Skip to content

Commit 85ca185

Browse files
committed
fix(BootstrapBlazorErrorBoundary): improve BuildRenderTree logic to render error or fallback content (#6113)
- Only renders ChildContent when there is no exception. - Renders custom ErrorContent if provided, otherwise renders default error content.
1 parent 7065ff2 commit 85ca185

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace BootstrapBlazor.Components;
1212
/// <summary>
1313
/// 内部使用 自定义异常组件
1414
/// </summary>
15-
class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
15+
public class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
1616
{
1717
[Inject]
1818
[NotNull]
@@ -68,19 +68,18 @@ protected override async Task OnErrorAsync(Exception exception)
6868
/// <param name="builder"></param>
6969
protected override void BuildRenderTree(RenderTreeBuilder builder)
7070
{
71-
#if DEBUG
72-
// DEBUG 模式下显示异常堆栈信息到 UI 页面方便开发人员调试
73-
if (OnErrorHandleAsync == null)
71+
if (CurrentException is null)
7472
{
75-
var ex = CurrentException ?? _exception;
76-
if (ex != null)
77-
{
78-
_exception = null;
79-
builder.AddContent(0, ExceptionContent(ex));
80-
}
73+
builder.AddContent(0, ChildContent);
74+
}
75+
else if (ErrorContent is not null)
76+
{
77+
builder.AddContent(1, ErrorContent(CurrentException));
78+
}
79+
else
80+
{
81+
builder.AddContent(2, ExceptionContent(CurrentException));
8182
}
82-
#endif
83-
builder.AddContent(1, ChildContent);
8483
}
8584

8685
private Exception? _exception = null;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
namespace UnitTest.Components;
7+
8+
public class BootstrapBlazorErrorBoundaryTest : BootstrapBlazorTestBase
9+
{
10+
// 1. Test rendering of child content when there is no exception
11+
[Fact]
12+
public void ShouldRenderChildContent_WhenNoException()
13+
{
14+
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
15+
.AddChildContent("<p>Normal Content</p>")
16+
);
17+
cut.MarkupMatches("<p>Normal Content</p>");
18+
}
19+
20+
// 2. Test rendering custom error content when an exception is thrown and ErrorContent is set
21+
[Fact]
22+
public void ShouldRenderCustomErrorContent_WhenExceptionOccurs()
23+
{
24+
var errorContentRendered = false;
25+
26+
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
27+
.Add(p => p.ErrorContent, ex => builder =>
28+
{
29+
errorContentRendered = true;
30+
builder.OpenElement(0, "div");
31+
builder.AddAttribute(1, "class", "custom-error");
32+
builder.AddContent(2, $"Custom error caught: {ex.Message}");
33+
builder.CloseElement();
34+
})
35+
.AddChildContent<ExceptionThrowingComponent>()
36+
);
37+
38+
cut.MarkupMatches("""<div class="custom-error">Custom error caught: Boom!</div>""");
39+
Assert.True(errorContentRendered);
40+
}
41+
42+
// 3. Test rendering default error content when an exception is thrown and ErrorContent is null
43+
[Fact]
44+
public void ShouldRenderDefaultExceptionContent_WhenExceptionOccurs_AndErrorContentIsNull()
45+
{
46+
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
47+
.AddChildContent<ExceptionThrowingComponent>()
48+
);
49+
Assert.Contains("Boom!", cut.Markup);
50+
}
51+
52+
private class ExceptionThrowingComponent : ComponentBase
53+
{
54+
protected override void OnInitialized()
55+
{
56+
throw new InvalidOperationException("Boom!");
57+
}
58+
}
59+
}
60+

0 commit comments

Comments
 (0)