Skip to content

Commit dae37ad

Browse files
fix(BootstrapBlazorErrorBoundary): improve BuildRenderTree logic to render error or fallback content (#6114)
* 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. * test: 更新单元测试 * doc: 更新全局异常示例 * feat: 增加基类重置方法 * refactor: 消除警告信息 * chore: bump version 9.7.1-beta01 Co-Authored-By: John <[email protected]> * doc: 更新文档 * refactor: 恢复模板代码 --------- Co-authored-by: Argo Zhang <[email protected]> Co-authored-by: John <[email protected]>
1 parent b80e875 commit dae37ad

File tree

8 files changed

+65
-7
lines changed

8 files changed

+65
-7
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@page "/error-page"
2+
3+
<h3>ErrorPage</h3>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 BootstrapBlazor.Server.Components.Pages;
7+
8+
/// <summary>
9+
/// ErrorPage 组件用于测试全局异常处理功能
10+
/// </summary>
11+
public partial class ErrorPage
12+
{
13+
/// <summary>
14+
/// <inheritdoc/>
15+
/// </summary>
16+
protected override void OnInitialized()
17+
{
18+
base.OnInitialized();
19+
20+
var a = 1;
21+
var b = 0;
22+
23+
// 这里会抛出异常
24+
var c = a / b;
25+
}
26+
}

src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@
7272
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["DialogText"]" OnClick="OnShowDialog" />
7373
</DemoBlock>
7474

75+
<DemoBlock Title="@Localizer["PageErrorTitle"]" Introduction="@Localizer["PageErrorIntro"]" Name="Page">
76+
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["ButtonText"]" OnClick="OnGotoPage" />
77+
</DemoBlock>
78+
7579
<AttributeTable Items="@GetAttributes()" />

src/BootstrapBlazor.Server/Components/Samples/GlobalException.razor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace BootstrapBlazor.Server.Components.Samples;
1010
/// </summary>
1111
public partial class GlobalException
1212
{
13+
[Inject]
14+
[NotNull]
15+
private NavigationManager? NavigationManager { get; set; }
16+
1317
[Inject]
1418
[NotNull]
1519
private SwalService? SwalService { get; set; }
@@ -19,7 +23,6 @@ public partial class GlobalException
1923

2024
private static void OnClick()
2125
{
22-
// NET6.0 采用 ErrorLogger 统一处理
2326
var a = 0;
2427
_ = 1 / a;
2528
}
@@ -39,6 +42,12 @@ private Task OnShowDialog() => DialogService.Show(new DialogOption()
3942
Component = BootstrapDynamicComponent.CreateComponent<MockError>()
4043
});
4144

45+
private Task OnGotoPage()
46+
{
47+
NavigationManager.NavigateTo("/error-page");
48+
return Task.CompletedTask;
49+
}
50+
4251
/// <summary>
4352
/// 获得属性方法
4453
/// </summary>

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,9 @@
17251725
"Block2Intro": "Set custom exception handling logic by setting <code>OnErrorHandleAsync</code> callback method",
17261726
"DialogTitle": "In Dialog",
17271727
"DialogIntro": "Click the button to pop up a pop-up window. The button in the pop-up window triggers an exception and the error is displayed in the pop-up window",
1728-
"DialogText": "Popup"
1728+
"DialogText": "Popup",
1729+
"PageErrorTitle": "Page",
1730+
"PageErrorIntro": "Click the button to navigate to the page where the error occurred during the page life cycle. The error is displayed on the current page and does not affect the menu and the overall page layout."
17291731
},
17301732
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17311733
"Title": "Global exception",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,9 @@
17251725
"Block2Intro": "通过设置 <code>OnErrorHandleAsync</code> 回调方法,设置自定义异常处理逻辑",
17261726
"DialogTitle": "弹窗中异常捕获",
17271727
"DialogIntro": "点击按钮弹出弹窗,弹窗内按钮触发异常,错误显示在弹窗内",
1728-
"DialogText": "弹窗"
1728+
"DialogText": "弹窗",
1729+
"PageErrorTitle": "页面异常捕获",
1730+
"PageErrorIntro": "点击按钮导航到页面生命周期内出错的页面,错误显示在当前页面,不影响菜单以及整体页面布局"
17291731
},
17301732
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17311733
"Title": "全局配置",

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.7.0</Version>
4+
<Version>9.7.1-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Components.Rendering;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.Logging;
9+
using System.Reflection;
910

1011
namespace BootstrapBlazor.Components;
1112

@@ -71,10 +72,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
7172
var ex = CurrentException ?? _exception;
7273
if (ex != null)
7374
{
74-
_exception = null;
75-
7675
// 处理自定义异常逻辑
77-
if(OnErrorHandleAsync != null)
76+
if (OnErrorHandleAsync != null)
7877
{
7978
// 页面生命周期内异常直接调用这里
8079
_ = OnErrorHandleAsync(Logger, ex);
@@ -83,6 +82,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
8382

8483
// 渲染异常内容
8584
builder.AddContent(0, ExceptionContent(ex));
85+
86+
// 重置 CurrentException
87+
ResetException();
8688
}
8789
else
8890
{
@@ -91,6 +93,16 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
9193
}
9294
}
9395

96+
private PropertyInfo? _currentExceptionPropertyInfo;
97+
98+
private void ResetException()
99+
{
100+
_exception = null;
101+
102+
_currentExceptionPropertyInfo ??= GetType().BaseType!.GetProperty(nameof(CurrentException), BindingFlags.NonPublic | BindingFlags.Instance)!;
103+
_currentExceptionPropertyInfo.SetValue(this, null);
104+
}
105+
94106
private Exception? _exception = null;
95107

96108
private RenderFragment<Exception> ExceptionContent => ex => builder =>

0 commit comments

Comments
 (0)