Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace BootstrapBlazor.Components;
/// <summary>
/// 内部使用 自定义异常组件
/// </summary>
class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
public class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
{
[Inject]
[NotNull]
Expand Down Expand Up @@ -68,19 +68,18 @@ protected override async Task OnErrorAsync(Exception exception)
/// <param name="builder"></param>
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
#if DEBUG
// DEBUG 模式下显示异常堆栈信息到 UI 页面方便开发人员调试
if (OnErrorHandleAsync == null)
if (CurrentException is null)
{
var ex = CurrentException ?? _exception;
if (ex != null)
{
_exception = null;
builder.AddContent(0, ExceptionContent(ex));
}
builder.AddContent(0, ChildContent);
}
else if (ErrorContent is not null)
{
builder.AddContent(1, ErrorContent(CurrentException));
}
else
{
builder.AddContent(2, ExceptionContent(CurrentException));
}
#endif
builder.AddContent(1, ChildContent);
}

private Exception? _exception = null;
Expand Down
60 changes: 60 additions & 0 deletions test/UnitTest/Components/BootstrapBlazorErrorBoundaryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

namespace UnitTest.Components;

public class BootstrapBlazorErrorBoundaryTest : BootstrapBlazorTestBase
{
// 1. Test rendering of child content when there is no exception
[Fact]
public void ShouldRenderChildContent_WhenNoException()
{
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.AddChildContent("<p>Normal Content</p>")
);
cut.MarkupMatches("<p>Normal Content</p>");
}

// 2. Test rendering custom error content when an exception is thrown and ErrorContent is set
[Fact]
public void ShouldRenderCustomErrorContent_WhenExceptionOccurs()
{
var errorContentRendered = false;

var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.Add(p => p.ErrorContent, ex => builder =>
{
errorContentRendered = true;
builder.OpenElement(0, "div");
builder.AddAttribute(1, "class", "custom-error");
builder.AddContent(2, $"Custom error caught: {ex.Message}");
builder.CloseElement();
})
.AddChildContent<ExceptionThrowingComponent>()
);

cut.MarkupMatches("""<div class="custom-error">Custom error caught: Boom!</div>""");
Assert.True(errorContentRendered);
}

// 3. Test rendering default error content when an exception is thrown and ErrorContent is null
[Fact]
public void ShouldRenderDefaultExceptionContent_WhenExceptionOccurs_AndErrorContentIsNull()
{
var cut = Context.RenderComponent<BootstrapBlazorErrorBoundary>(parameters => parameters
.AddChildContent<ExceptionThrowingComponent>()
);
Assert.Contains("Boom!", cut.Markup);
}

private class ExceptionThrowingComponent : ComponentBase
{
protected override void OnInitialized()
{
throw new InvalidOperationException("Boom!");
}
}
}

Loading