Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

<section class="main">
<Wwads />
@Body
<ErrorLogger>
@Body
</ErrorLogger>
</section>

<DialButton DialMode="DialMode.Radial" Icon="fa-solid fa-gear" Radius="100" Placement="Placement.BottomEnd" class="bb-dial-gear">
Expand Down
3 changes: 3 additions & 0 deletions src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@page "/error-page"

<h3>ErrorPage</h3>
26 changes: 26 additions & 0 deletions src/BootstrapBlazor.Server/Components/Pages/ErrorPage.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 BootstrapBlazor.Server.Components.Pages;

/// <summary>
/// ErrorPage 组件用于测试全局异常处理功能
/// </summary>
public partial class ErrorPage
{
/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnInitialized()
{
base.OnInitialized();

var a = 1;
var b = 0;

// 这里会抛出异常
var c = 1 / b;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["DialogText"]" OnClick="OnShowDialog" />
</DemoBlock>

<DemoBlock Title="@Localizer["PageErrorTitle"]" Introduction="@Localizer["PageErrorIntro"]" Name="Page">
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["ButtonText"]" OnClick="OnGotoPage" />
</DemoBlock>

<AttributeTable Items="@GetAttributes()" />
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace BootstrapBlazor.Server.Components.Samples;
/// </summary>
public partial class GlobalException
{
[Inject]
[NotNull]
private NavigationManager? NavigationManager { get; set; }

[Inject]
[NotNull]
private SwalService? SwalService { get; set; }
Expand All @@ -19,7 +23,6 @@ public partial class GlobalException

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

private Task OnGotoPage()
{
NavigationManager.NavigateTo("/error-page");
return Task.CompletedTask;
}

/// <summary>
/// 获得属性方法
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,9 @@
"Block2Intro": "Set custom exception handling logic by setting <code>OnErrorHandleAsync</code> callback method",
"DialogTitle": "In Dialog",
"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",
"DialogText": "Popup"
"DialogText": "Popup",
"PageErrorTitle": "Page",
"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."
},
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
"Title": "Global exception",
Expand Down
4 changes: 3 additions & 1 deletion src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,9 @@
"Block2Intro": "通过设置 <code>OnErrorHandleAsync</code> 回调方法,设置自定义异常处理逻辑",
"DialogTitle": "弹窗中异常捕获",
"DialogIntro": "点击按钮弹出弹窗,弹窗内按钮触发异常,错误显示在弹窗内",
"DialogText": "弹窗"
"DialogText": "弹窗",
"PageErrorTitle": "页面异常捕获",
"PageErrorIntro": "点击按钮导航到页面生命周期内出错的页面,错误显示在当前页面,不影响菜单以及整体页面布局"
},
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
"Title": "全局配置",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Reflection;

namespace BootstrapBlazor.Components;

Expand Down Expand Up @@ -71,10 +72,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
var ex = CurrentException ?? _exception;
if (ex != null)
{
_exception = null;

// 处理自定义异常逻辑
if(OnErrorHandleAsync != null)
if (OnErrorHandleAsync != null)
{
// 页面生命周期内异常直接调用这里
_ = OnErrorHandleAsync(Logger, ex);
Expand All @@ -83,6 +82,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

// 渲染异常内容
builder.AddContent(0, ExceptionContent(ex));

// 重置 CurrentException
ResetException();
}
else
{
Expand All @@ -91,6 +93,16 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
}
}

private PropertyInfo? _currentExceptionPropertyInfo;

private void ResetException()
{
_exception = null;

_currentExceptionPropertyInfo ??= GetType().BaseType!.GetProperty(nameof(CurrentException), BindingFlags.NonPublic | BindingFlags.Instance)!;
_currentExceptionPropertyInfo.SetValue(this, null);
}

private Exception? _exception = null;

private RenderFragment<Exception> ExceptionContent => ex => builder =>
Expand Down