diff --git a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs index f814abcdfd0..40071f3316f 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/BootstrapBlazorErrorBoundary.cs @@ -61,16 +61,14 @@ protected override async Task OnErrorAsync(Exception exception) if (OnErrorHandleAsync != null) { await OnErrorHandleAsync(Logger, exception); + return; } - else - { - if (ShowToast) - { - await ToastService.Error(ToastTitle, exception.Message); - } - Logger.LogError(exception, "{BootstrapBlazorErrorBoundary} {OnErrorAsync} log this error occurred at {Page}", nameof(BootstrapBlazorErrorBoundary), nameof(OnErrorAsync), NavigationManager.Uri); + if (ShowToast) + { + await ToastService.Error(ToastTitle, exception.Message); } + Logger.LogError(exception, "{BootstrapBlazorErrorBoundary} {OnErrorAsync} log this error occurred at {Page}", nameof(BootstrapBlazorErrorBoundary), nameof(OnErrorAsync), NavigationManager.Uri); } /// @@ -126,16 +124,14 @@ private MarkupString GetErrorContentMarkupString(Exception ex) /// public async Task RenderException(Exception exception, IHandlerException? handler) { - await OnErrorAsync(exception); - if (handler != null) { await handler.HandlerException(exception, ExceptionContent); + return; } - else - { - _exception = exception; - StateHasChanged(); - } + + await OnErrorAsync(exception); + _exception = exception; + StateHasChanged(); } } diff --git a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs index 8905116bf76..2a954f2c909 100644 --- a/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs +++ b/src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs @@ -56,7 +56,8 @@ public class ErrorLogger : ComponentBase, IErrorLogger [Parameter] public RenderFragment? ErrorContent { get; set; } - private BootstrapBlazorErrorBoundary _errorBoundary = default!; + [NotNull] + private BootstrapBlazorErrorBoundary? _errorBoundary = default; /// /// diff --git a/test/UnitTest/Components/ErrorHandlerTest.cs b/test/UnitTest/Components/ErrorHandlerTest.cs index d48db6ccdc7..d604b06e228 100644 --- a/test/UnitTest/Components/ErrorHandlerTest.cs +++ b/test/UnitTest/Components/ErrorHandlerTest.cs @@ -30,13 +30,31 @@ await cut.InvokeAsync(() => dialog.Show(new DialogOption() // 关闭弹窗 var btn = cut.Find(".modal-header .btn-close"); await cut.InvokeAsync(() => btn.Click()); + } + [Fact] + public async Task ShowToast_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent(); + }); + var errorButton = cut.Find(".btn-error"); + await cut.InvokeAsync(() => errorButton.Click()); cut.Contains("
test error logger
"); // 关闭 Toast var toast = cut.FindComponent().Instance; await cut.InvokeAsync(() => toast.Close()); cut.DoesNotContain("
test error logger
"); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.ShowToast, false); + }); + errorButton = cut.Find(".btn-error"); + await cut.InvokeAsync(() => errorButton.Click()); + cut.DoesNotContain("
test error logger
"); } private class MockDialogTest : ComponentBase