Skip to content

Commit 40bea27

Browse files
authored
feat(IErrorLogger): support DetailedErrors config (#4579)
* doc: 增加弹窗示例 * refactor: 优化代码 * refactor: 增加异常提示信息栏 * doc: 增加示例文档 * refactor: 增加 DetailedErrors 配置项检查 * test: 更新单元测试
1 parent 2fb6b5e commit 40bea27

File tree

9 files changed

+77
-14
lines changed

9 files changed

+77
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Button Icon="fa-solid fa-font-awesome" Text="Click" OnClick="OnClick" />
2+
3+
@code {
4+
private static void OnClick()
5+
{
6+
// NET6.0 采用 ErrorLogger 统一处理
7+
var a = 0;
8+
_ = 1 / a;
9+
}
10+
}

src/BootstrapBlazor.Server/Components/Layout/BaseLayout.razor

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66
@Body
77
</main>
88
</BootstrapBlazorRoot>
9+
10+
<div id="blazor-error-ui">
11+
An unhandled error has occurred.
12+
<a href="" class="reload">Reload</a>
13+
<a class="dismiss">🗙</a>
14+
</div>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@
5959

6060
<DemoBlock Title="@Localizer["Block1Title"]" Introduction="@Localizer["Block1Intro"]" Name="Normal">
6161
<section ignore>@((MarkupString)Localizer["ExceptionTestIntroduce"].Value)</section>
62-
<ErrorLogger>
63-
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["ButtonText"]" OnClick="OnClick" />
64-
</ErrorLogger>
62+
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["ButtonText"]" OnClick="OnClick" />
6563
</DemoBlock>
6664

6765
<DemoBlock Title="@Localizer["Block2Title"]" Introduction="@Localizer["Block2Intro"]" Name="Swal">
@@ -70,4 +68,8 @@
7068
</ErrorLogger>
7169
</DemoBlock>
7270

71+
<DemoBlock Title="@Localizer["DialogTitle"]" Introduction="@Localizer["DialogIntro"]" Name="Dialog">
72+
<Button Icon="fa-solid fa-font-awesome" Text="@Localizer["DialogText"]" OnClick="OnShowDialog" />
73+
</DemoBlock>
74+
7375
<AttributeTable Items="@GetAttributes()" />

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public partial class GlobalException
1414
[NotNull]
1515
private SwalService? SwalService { get; set; }
1616

17+
[Inject, NotNull]
18+
private DialogService? DialogService { get; set; }
19+
1720
private static void OnClick()
1821
{
1922
// NET6.0 采用 ErrorLogger 统一处理
@@ -30,6 +33,12 @@ private Task OnErrorHandleAsync(ILogger logger, Exception ex) => SwalService.Sho
3033
FooterTemplate = BootstrapDynamicComponent.CreateComponent<SwalFooter>().Render()
3134
});
3235

36+
private Task OnShowDialog() => DialogService.Show(new DialogOption()
37+
{
38+
Title = Localizer["DialogTitle"],
39+
Component = BootstrapDynamicComponent.CreateComponent<MockError>()
40+
});
41+
3342
/// <summary>
3443
/// 获得属性方法
3544
/// </summary>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,10 @@
17411741
"ExceptionTestIntroduce": "In this example code, an error code that divides by zero is written. Because <code>try/catch</code> is used to capture the exception, the error message is displayed in the console below",
17421742
"ButtonText": "test",
17431743
"Block2Title": "OnErrorHandleAsync",
1744-
"Block2Intro": "Set custom exception handling logic by setting <code>OnErrorHandleAsync</code> callback method"
1744+
"Block2Intro": "Set custom exception handling logic by setting <code>OnErrorHandleAsync</code> callback method",
1745+
"DialogTitle": "In Dialog",
1746+
"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",
1747+
"DialogText": "Popup"
17451748
},
17461749
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17471750
"Title": "Global exception",

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,10 @@
17411741
"ExceptionTestIntroduce": "本例代码中写了一个除以零的错误代码,并且未使用 <code>try/catch</code> 对异常进行捕获,系统并不会崩溃导致不可用,<ul class='ul-demo'><li><code>Debug</code> 模式下会显示错误的 <b>描述信息</b> 与 <b>堆栈信息</b></li><li><code>Release</code> 模式下默认使用 <code>Toast</code> 进行弹窗提示</li></ul>",
17421742
"ButtonText": "测试",
17431743
"Block2Title": "自定义错误处理",
1744-
"Block2Intro": "通过设置 <code>OnErrorHandleAsync</code> 回调方法,设置自定义异常处理逻辑"
1744+
"Block2Intro": "通过设置 <code>OnErrorHandleAsync</code> 回调方法,设置自定义异常处理逻辑",
1745+
"DialogTitle": "弹窗中异常捕获",
1746+
"DialogIntro": "点击按钮弹出弹窗,弹窗内按钮触发异常,错误显示在弹窗内",
1747+
"DialogText": "弹窗"
17451748
},
17461749
"BootstrapBlazor.Server.Components.Pages.GlobalOption": {
17471750
"Title": "全局配置",

src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
8080
var ex = CurrentException ?? _exception;
8181
if (ex != null)
8282
{
83+
_exception = null;
8384
builder.AddContent(0, ExceptionContent(ex));
8485
}
8586
}
@@ -99,11 +100,21 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
99100
var index = 0;
100101
builder.OpenElement(index++, "div");
101102
builder.AddAttribute(index++, "class", "error-stack");
102-
builder.AddContent(index++, ex.FormatMarkupString(Configuration.GetEnvironmentInformation()));
103+
builder.AddContent(index++, GetErrorContentMarkupString(ex));
103104
builder.CloseElement();
104105
}
105106
};
106107

108+
private bool? _errorDetails;
109+
110+
private MarkupString GetErrorContentMarkupString(Exception ex)
111+
{
112+
_errorDetails ??= Configuration.GetValue("DetailedErrors", false);
113+
return _errorDetails is true
114+
? ex.FormatMarkupString(Configuration.GetEnvironmentInformation())
115+
: new MarkupString(ex.Message);
116+
}
117+
107118
/// <summary>
108119
/// 渲染异常信息方法
109120
/// </summary>

src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,7 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
9999
/// </summary>
100100
/// <param name="exception"></param>
101101
/// <returns></returns>
102-
public async Task HandlerExceptionAsync(Exception exception)
103-
{
104-
if (EnableErrorLogger)
105-
{
106-
await _errorBoundary.RenderException(exception, _cache.LastOrDefault());
107-
}
108-
}
102+
public Task HandlerExceptionAsync(Exception exception) => _errorBoundary.RenderException(exception, _cache.LastOrDefault());
109103

110104
private readonly List<IHandlerException> _cache = [];
111105

test/UnitTest/Components/ErrorLoggerTest.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.AspNetCore.Components.Web;
7+
using Microsoft.Extensions.Configuration;
78

89
namespace UnitTest.Components;
910

@@ -39,6 +40,31 @@ public void OnErrorAsync_Ok()
3940
button.TriggerEvent("onclick", EventArgs.Empty);
4041
}
4142

43+
[Fact]
44+
public async Task DetailedErrors_False()
45+
{
46+
var config = Context.Services.GetRequiredService<IConfiguration>();
47+
config["DetailedErrors"] = "false";
48+
49+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
50+
{
51+
pb.AddChildContent<ErrorLogger>(pb =>
52+
{
53+
pb.Add(e => e.ShowToast, false);
54+
pb.AddChildContent<Button>(pb =>
55+
{
56+
pb.Add(b => b.OnClick, () =>
57+
{
58+
var a = 0;
59+
_ = 1 / a;
60+
});
61+
});
62+
});
63+
});
64+
var button = cut.Find("button");
65+
await cut.InvokeAsync(() => button.Click());
66+
}
67+
4268
[Fact]
4369
public async Task OnErrorHandleAsync_Ok()
4470
{
@@ -96,7 +122,6 @@ public void OnErrorHandleAsync_Tab()
96122
cut.Contains("errorLogger-click");
97123
var button = cut.Find("button");
98124
button.TriggerEvent("onclick", EventArgs.Empty);
99-
100125
cut.Contains("<div class=\"tabs-body-content\"><div class=\"error-stack\">TimeStamp:");
101126
}
102127

0 commit comments

Comments
 (0)