Skip to content

Commit 20bebc4

Browse files
committed
Merge branch 'main' into feat-upload
2 parents 478ad07 + dae37ad commit 20bebc4

File tree

18 files changed

+273
-47
lines changed

18 files changed

+273
-47
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/BaseComponents/BootstrapBlazorRoot.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
@namespace BootstrapBlazor.Components
33

44
<CascadingValue Value="this" IsFixed="true">
5-
<ErrorLogger EnableErrorLogger="EnableErrorLoggerValue" ShowToast="ShowToast" ToastTitle="@ToastTitle" OnErrorHandleAsync="OnErrorHandleAsync!">
5+
<ErrorLogger EnableErrorLogger="_enableErrorLoggerValue" ShowToast="_showToast" ToastTitle="@ToastTitle"
6+
OnErrorHandleAsync="OnErrorHandleAsync">
67
@ChildContent
78

89
<Dialog></Dialog>

src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public partial class BootstrapBlazorRoot
4848
public Func<ILogger, Exception, Task>? OnErrorHandleAsync { get; set; }
4949

5050
/// <summary>
51-
/// 获得/设置 是否显示 Error 提示弹窗 默认 true 显示
51+
/// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 <see cref="BootstrapBlazorOptions.ShowErrorLoggerToast"/> 设置值
5252
/// </summary>
5353
[Parameter]
54-
public bool ShowToast { get; set; } = true;
54+
public bool? ShowToast { get; set; }
5555

5656
/// <summary>
5757
/// 获得/设置 Error Toast 弹窗标题
@@ -65,7 +65,9 @@ public partial class BootstrapBlazorRoot
6565
[Parameter]
6666
public bool? EnableErrorLogger { get; set; }
6767

68-
private bool EnableErrorLoggerValue => EnableErrorLogger ?? Options.CurrentValue.EnableErrorLogger;
68+
private bool _enableErrorLoggerValue => EnableErrorLogger ?? Options.CurrentValue.EnableErrorLogger;
69+
70+
private bool _showToast => ShowToast ?? Options.CurrentValue.ShowErrorLoggerToast;
6971

7072
/// <summary>
7173
/// SetParametersAsync 方法

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 29 additions & 9 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

@@ -68,19 +69,38 @@ protected override async Task OnErrorAsync(Exception exception)
6869
/// <param name="builder"></param>
6970
protected override void BuildRenderTree(RenderTreeBuilder builder)
7071
{
71-
#if DEBUG
72-
// DEBUG 模式下显示异常堆栈信息到 UI 页面方便开发人员调试
73-
if (OnErrorHandleAsync == null)
72+
var ex = CurrentException ?? _exception;
73+
if (ex != null)
7474
{
75-
var ex = CurrentException ?? _exception;
76-
if (ex != null)
75+
// 处理自定义异常逻辑
76+
if (OnErrorHandleAsync != null)
7777
{
78-
_exception = null;
79-
builder.AddContent(0, ExceptionContent(ex));
78+
// 页面生命周期内异常直接调用这里
79+
_ = OnErrorHandleAsync(Logger, ex);
80+
return;
8081
}
82+
83+
// 渲染异常内容
84+
builder.AddContent(0, ExceptionContent(ex));
85+
86+
// 重置 CurrentException
87+
ResetException();
88+
}
89+
else
90+
{
91+
// 渲染正常内容
92+
builder.AddContent(1, ChildContent);
8193
}
82-
#endif
83-
builder.AddContent(1, ChildContent);
94+
}
95+
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);
84104
}
85105

86106
private Exception? _exception = null;

0 commit comments

Comments
 (0)