diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor index 65cd12308b5..02cbda69d87 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor @@ -1,8 +1,9 @@ -@inherits ComponentBase +@inherits ComponentBase @namespace BootstrapBlazor.Components - @ChildContent diff --git a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs index 5a0446927a6..a9c1d1e19a6 100644 --- a/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs +++ b/src/BootstrapBlazor/Components/BaseComponents/BootstrapBlazorRoot.razor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -42,16 +42,30 @@ public partial class BootstrapBlazorRoot public ToastContainer? ToastContainer { get; private set; } /// - /// 获得/设置 自定义错误处理回调方法 + /// 获得/设置 是否开启全局异常捕获 默认 null 使用 设置值 /// [Parameter] - public Func? OnErrorHandleAsync { get; set; } + public bool? EnableErrorLogger { get; set; } + + /// + /// 获得/设置 是否记录异常到 默认 null 使用 设置值 + /// + [Parameter] + public bool? EnableErrorLoggerILogger { get; set; } + + /// + /// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 设置值 + /// + [Parameter] + [Obsolete("已弃用,请使用 ShowErrorLoggerToast 参数. Deprecated, please use ShowErrorLoggerToast parameter")] + [ExcludeFromCodeCoverage] + public bool? ShowToast { get => ShowErrorLoggerToast; set => ShowErrorLoggerToast = value; } /// /// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 设置值 /// [Parameter] - public bool? ShowToast { get; set; } + public bool? ShowErrorLoggerToast { get; set; } /// /// 获得/设置 Error Toast 弹窗标题 @@ -60,17 +74,19 @@ public partial class BootstrapBlazorRoot public string? ToastTitle { get; set; } /// - /// 获得/设置 是否开启全局异常捕获 默认 null 读取配置文件 EnableErrorLogger 值 + /// 获得/设置 自定义错误处理回调方法 /// [Parameter] - public bool? EnableErrorLogger { get; set; } + public Func? OnErrorHandleAsync { get; set; } private bool EnableErrorLoggerValue => EnableErrorLogger ?? Options.CurrentValue.EnableErrorLogger; - private bool ShowToastValue => ShowToast ?? Options.CurrentValue.ShowErrorLoggerToast; + private bool EnableErrorLoggerILoggerValue => EnableErrorLoggerILogger ?? Options.CurrentValue.EnableErrorLoggerILogger; + + private bool ShowToastValue => ShowErrorLoggerToast ?? Options.CurrentValue.ShowErrorLoggerToast; /// - /// SetParametersAsync 方法 + /// /// /// /// diff --git a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs index ef9163543ba..41c1f32d08d 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -43,16 +43,16 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase public Func? OnErrorHandleAsync { get; set; } /// - /// 获得/设置 是否显示弹窗 默认 true 显示 + /// 获得/设置 是否启用日志记录功能 默认 true 启用 /// [Parameter] - public bool ShowToast { get; set; } = true; + public bool EnableILogger { get; set; } = true; /// - /// 获得/设置 是否启用日志记录功能 默认 true 启用 + /// 获得/设置 是否显示弹窗 默认 true 显示 /// [Parameter] - public bool EnableILogger { get; set; } = true; + public bool ShowToast { get; set; } = true; /// /// 获得/设置 Toast 弹窗标题 @@ -164,7 +164,7 @@ public async Task RenderException(Exception exception, IHandlerException? handle if (HostEnvironment.IsDevelopment()) { // IHandlerException 处理异常逻辑 - await handler.HandlerException(exception, ExceptionContent); + await handler.HandlerExceptionAsync(exception, ExceptionContent); } else { diff --git a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs index 091cb2e590b..b7ff466c946 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs @@ -28,13 +28,13 @@ public class ErrorLogger : ComponentBase, IErrorLogger /// /// [Parameter] - public bool ShowToast { get; set; } = true; + public bool EnableILogger { get; set; } = true; /// /// /// [Parameter] - public bool EnableILogger { get; set; } = true; + public bool ShowToast { get; set; } = true; /// /// @@ -44,7 +44,7 @@ public class ErrorLogger : ComponentBase, IErrorLogger public string? ToastTitle { get; set; } /// - /// + /// 获得/设置 自定义错误处理回调方法 /// [Parameter] public Func? OnErrorHandleAsync { get; set; } diff --git a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs index b50de90a597..0fe63bf1ed6 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -16,11 +16,10 @@ public interface IErrorLogger bool EnableErrorLogger { get; set; } /// - /// 获得/设置 自定义 Error 处理方法 默认 null + /// 获得/设置 是否启用日志记录功能 默认 true 启用 + /// 设置 false 后关闭记录日志功能 /// - /// - /// - Task HandlerExceptionAsync(Exception ex); + bool EnableILogger { get; } /// /// 获得 是否显示 Error 提示弹窗 默认 true @@ -28,15 +27,15 @@ public interface IErrorLogger bool ShowToast { get; } /// - /// 获得/设置 是否启用日志记录功能 默认 true 启用 - /// 设置 false 后关闭记录日志功能 + /// 获得 Error Toast 弹窗标题 默认读取资源文件内容 /// - bool EnableILogger { get; } + string? ToastTitle { get; } /// - /// 获得 Error Toast 弹窗标题 默认读取资源文件内容 + /// 获得/设置 自定义 Error 处理方法 默认 null /// - string? ToastTitle { get; } + /// + Task HandlerExceptionAsync(Exception ex); /// /// 注册方法 diff --git a/src/BootstrapBlazor/Components/ErrorLogger/IHandlerException.cs b/src/BootstrapBlazor/Components/ErrorLogger/IHandlerException.cs index bd5e39d837e..a7fd73d7bce 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/IHandlerException.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/IHandlerException.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -6,14 +6,14 @@ namespace BootstrapBlazor.Components; /// -/// IHandlerException +/// IHandlerException 接口 /// public interface IHandlerException { /// - /// + /// 处理异常方法 /// /// /// - Task HandlerException(Exception ex, RenderFragment errorContent); + Task HandlerExceptionAsync(Exception ex, RenderFragment errorContent); } diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor b/src/BootstrapBlazor/Components/Layout/Layout.razor index 80df44fc010..10251c19545 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor @@ -1,4 +1,4 @@ -@namespace BootstrapBlazor.Components +@namespace BootstrapBlazor.Components @inherits BootstrapModuleComponentBase @attribute [BootstrapModuleAutoLoader(JSObjectReference = true)] @@ -126,8 +126,9 @@ } else { - + @HandlerMain() } diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs index 81ae8864981..cda1f184563 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -473,22 +473,22 @@ public partial class Layout : IHandlerException, ITabHeader public object? Resource { get; set; } /// - /// 获得/设置 是否开启全局异常捕获 默认 null 读取配置文件 EnableErrorLogger 值 + /// 获得/设置 是否开启全局异常捕获 默认 null 使用 设置值 /// [Parameter] public bool? EnableErrorLogger { get; set; } /// - /// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 设置值 + /// 获得/设置 是否记录异常到 默认 null 使用 设置值 /// [Parameter] - public bool? ShowErrorLoggerToast { get; set; } + public bool? EnableErrorLoggerILogger { get; set; } /// - /// 获得/设置 是否启用日志记录功能 默认 null 启用 使用 设置值 + /// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 设置值 /// [Parameter] - public bool? EnableErrorLoggerILogger { get; set; } + public bool? ShowErrorLoggerToast { get; set; } /// /// 获得/设置 错误日志 弹窗标题 默认 null @@ -526,10 +526,10 @@ public partial class Layout : IHandlerException, ITabHeader private bool EnableLogger => EnableErrorLogger ?? Options.CurrentValue.EnableErrorLogger; - private bool ShowToast => ShowErrorLoggerToast ?? Options.CurrentValue.ShowErrorLoggerToast; - private bool EnableILogger => EnableErrorLoggerILogger ?? Options.CurrentValue.EnableErrorLoggerILogger; + private bool ShowToast => ShowErrorLoggerToast ?? Options.CurrentValue.ShowErrorLoggerToast; + /// /// /// @@ -702,7 +702,7 @@ private Task OnErrorLoggerInitialized(ErrorLogger logger) /// /// /// - public virtual Task HandlerException(Exception ex, RenderFragment errorContent) + public virtual Task HandlerExceptionAsync(Exception ex, RenderFragment errorContent) { _errorContent = errorContent(ex); StateHasChanged(); diff --git a/src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs b/src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs index 10df7c094b8..ea16c5d5324 100644 --- a/src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs +++ b/src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// 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(argo@live.ca) Website: https://www.blazor.zone @@ -453,7 +453,7 @@ private RenderFragment RenderBodyTemplate() => builder => /// /// /// - public virtual Task HandlerException(Exception ex, RenderFragment errorContent) + public virtual Task HandlerExceptionAsync(Exception ex, RenderFragment errorContent) { _errorContent = errorContent(ex); StateHasChanged(); diff --git a/src/BootstrapBlazor/Components/Tab/TabItemContent.cs b/src/BootstrapBlazor/Components/Tab/TabItemContent.cs index 54c4c9ef521..8e82bf96383 100644 --- a/src/BootstrapBlazor/Components/Tab/TabItemContent.cs +++ b/src/BootstrapBlazor/Components/Tab/TabItemContent.cs @@ -88,7 +88,7 @@ public void Render() /// /// /// - public Task HandlerException(Exception ex, RenderFragment errorContent) => DialogService.ShowErrorHandlerDialog(errorContent(ex)); + public Task HandlerExceptionAsync(Exception ex, RenderFragment errorContent) => DialogService.ShowErrorHandlerDialog(errorContent(ex)); /// /// IDispose 方法用于释放资源 diff --git a/test/UnitTest/Components/BootstrapBlazorRootTest.cs b/test/UnitTest/Components/BootstrapBlazorRootTest.cs index 376bde70f0e..327919fc993 100644 --- a/test/UnitTest/Components/BootstrapBlazorRootTest.cs +++ b/test/UnitTest/Components/BootstrapBlazorRootTest.cs @@ -5,8 +5,6 @@ //using HarmonyLib; -using Bunit.TestDoubles; - namespace UnitTest.Components; public class BootstrapBlazorRootTest : TestBase @@ -27,7 +25,6 @@ class MockGenerator : IRootComponentGenerator /// /// /// - /// public RenderFragment Generator() => builder => { builder.AddContent(0, new MarkupString("
")); diff --git a/test/UnitTest/Components/ErrorLoggerTest.cs b/test/UnitTest/Components/ErrorLoggerTest.cs index 92363722671..5a829542e7a 100644 --- a/test/UnitTest/Components/ErrorLoggerTest.cs +++ b/test/UnitTest/Components/ErrorLoggerTest.cs @@ -13,7 +13,7 @@ namespace UnitTest.Components; public class ErrorLoggerTest : BootstrapBlazorTestBase { [Fact] - public void OnErrorAsync_Ok() + public async Task OnErrorAsync_Ok() { var cut = Context.Render(pb => { @@ -39,7 +39,7 @@ public void OnErrorAsync_Ok() pb.Add(e => e.ShowToast, true); }); var button = cut.Find("button"); - button.TriggerEvent("onclick", EventArgs.Empty); + await cut.InvokeAsync(() => button.Click()); } [Fact] @@ -130,13 +130,14 @@ public void OnErrorHandleAsync_Tab() } [Fact] - public void Root_Ok() + public async Task Root_Ok() { Exception? exception = null; var cut = Context.Render(pb => { pb.Add(a => a.EnableErrorLogger, true); - pb.Add(a => a.ShowToast, false); + pb.Add(a => a.EnableErrorLoggerILogger, true); + pb.Add(a => a.ShowErrorLoggerToast, false); pb.Add(a => a.ToastTitle, "Test"); pb.Add(a => a.OnErrorHandleAsync, (logger, ex) => { @@ -153,17 +154,24 @@ public void Root_Ok() }); }); var button = cut.Find("button"); - cut.InvokeAsync(() => button.Click()); + await cut.InvokeAsync(() => button.Click()); Assert.NotNull(exception); + + cut.Render(pb => + { + pb.Add(a => a.EnableErrorLogger, false); + }); + button = cut.Find("button"); + await Assert.ThrowsAsync(async () => await cut.InvokeAsync(() => button.Click())); } [Fact] - public void ErrorContent_Ok() + public async Task ErrorContent_Ok() { var cut = Context.Render(pb => { pb.Add(a => a.EnableErrorLogger, true); - pb.Add(a => a.ShowToast, true); + pb.Add(a => a.ShowErrorLoggerToast, true); pb.AddChildContent