Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@inherits ComponentBase
@inherits ComponentBase
@namespace BootstrapBlazor.Components

<CascadingValue Value="this" IsFixed="true">
<ErrorLogger EnableErrorLogger="EnableErrorLoggerValue" ShowToast="ShowToastValue" ToastTitle="@ToastTitle"
<ErrorLogger EnableErrorLogger="EnableErrorLoggerValue" EnableILogger="EnableErrorLoggerILoggerValue"
ShowToast="ShowToastValue" ToastTitle="@ToastTitle"
OnErrorHandleAsync="OnErrorHandleAsync">
@ChildContent

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -42,16 +42,30 @@ public partial class BootstrapBlazorRoot
public ToastContainer? ToastContainer { get; private set; }

/// <summary>
/// 获得/设置 自定义错误处理回调方法
/// 获得/设置 是否开启全局异常捕获 默认 null 使用 <see cref="BootstrapBlazorOptions.EnableErrorLogger"/> 设置值
/// </summary>
[Parameter]
public Func<ILogger, Exception, Task>? OnErrorHandleAsync { get; set; }
public bool? EnableErrorLogger { get; set; }

/// <summary>
/// 获得/设置 是否记录异常到 <see cref="ILogger"/> 默认 null 使用 <see cref="BootstrapBlazorOptions.EnableErrorLoggerILogger"/> 设置值
/// </summary>
[Parameter]
public bool? EnableErrorLoggerILogger { get; set; }

/// <summary>
/// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 <see cref="BootstrapBlazorOptions.ShowErrorLoggerToast"/> 设置值
/// </summary>
[Parameter]
[Obsolete("已弃用,请使用 ShowErrorLoggerToast 参数. Deprecated, please use ShowErrorLoggerToast parameter")]
[ExcludeFromCodeCoverage]
public bool? ShowToast { get => ShowErrorLoggerToast; set => ShowErrorLoggerToast = value; }

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

/// <summary>
/// 获得/设置 Error Toast 弹窗标题
Expand All @@ -60,17 +74,19 @@ public partial class BootstrapBlazorRoot
public string? ToastTitle { get; set; }

/// <summary>
/// 获得/设置 是否开启全局异常捕获 默认 null 读取配置文件 EnableErrorLogger 值
/// 获得/设置 自定义错误处理回调方法
/// </summary>
[Parameter]
public bool? EnableErrorLogger { get; set; }
public Func<ILogger, Exception, Task>? 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;

/// <summary>
/// SetParametersAsync 方法
/// <inheritdoc/>
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,16 +43,16 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
public Func<ILogger, Exception, Task>? OnErrorHandleAsync { get; set; }

/// <summary>
/// 获得/设置 是否显示弹窗 默认 true 显示
/// 获得/设置 是否启用日志记录功能 默认 true 启用
/// </summary>
[Parameter]
public bool ShowToast { get; set; } = true;
public bool EnableILogger { get; set; } = true;

/// <summary>
/// 获得/设置 是否启用日志记录功能 默认 true 启用
/// 获得/设置 是否显示弹窗 默认 true 显示
/// </summary>
[Parameter]
public bool EnableILogger { get; set; } = true;
public bool ShowToast { get; set; } = true;

/// <summary>
/// 获得/设置 Toast 弹窗标题
Expand Down Expand Up @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Components/ErrorLogger/ErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class ErrorLogger : ComponentBase, IErrorLogger
/// <inheritdoc/>
/// </summary>
[Parameter]
public bool ShowToast { get; set; } = true;
public bool EnableILogger { get; set; } = true;

/// <summary>
/// <inheritdoc/>
/// </summary>
[Parameter]
public bool EnableILogger { get; set; } = true;
public bool ShowToast { get; set; } = true;

/// <summary>
/// <inheritdoc/>
Expand All @@ -44,7 +44,7 @@ public class ErrorLogger : ComponentBase, IErrorLogger
public string? ToastTitle { get; set; }

/// <summary>
/// <inheritdoc/>
/// 获得/设置 自定义错误处理回调方法
/// </summary>
[Parameter]
public Func<ILogger, Exception, Task>? OnErrorHandleAsync { get; set; }
Expand Down
19 changes: 9 additions & 10 deletions src/BootstrapBlazor/Components/ErrorLogger/IErrorLogger.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,27 +16,26 @@ public interface IErrorLogger
bool EnableErrorLogger { get; set; }

/// <summary>
/// 获得/设置 自定义 Error 处理方法 默认 null
/// 获得/设置 是否启用日志记录功能 默认 true 启用
/// <para>设置 false 后关闭记录日志功能</para>
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
Task HandlerExceptionAsync(Exception ex);
bool EnableILogger { get; }

/// <summary>
/// 获得 是否显示 Error 提示弹窗 默认 true
/// </summary>
bool ShowToast { get; }

/// <summary>
/// 获得/设置 是否启用日志记录功能 默认 true 启用
/// <para>设置 false 后关闭记录日志功能</para>
/// 获得 Error Toast 弹窗标题 默认读取资源文件内容
/// </summary>
bool EnableILogger { get; }
string? ToastTitle { get; }

/// <summary>
/// 获得 Error Toast 弹窗标题 默认读取资源文件内容
/// 获得/设置 自定义 Error 处理方法 默认 null
/// </summary>
string? ToastTitle { get; }
/// <param name="ex"></param>
Task HandlerExceptionAsync(Exception ex);

/// <summary>
/// 注册方法
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// 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

namespace BootstrapBlazor.Components;

/// <summary>
/// IHandlerException
/// IHandlerException 接口
/// </summary>
public interface IHandlerException
{
/// <summary>
///
/// 处理异常方法
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
Task HandlerException(Exception ex, RenderFragment<Exception> errorContent);
Task HandlerExceptionAsync(Exception ex, RenderFragment<Exception> errorContent);
}
7 changes: 4 additions & 3 deletions src/BootstrapBlazor/Components/Layout/Layout.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@namespace BootstrapBlazor.Components
@namespace BootstrapBlazor.Components
@inherits BootstrapModuleComponentBase
@attribute [BootstrapModuleAutoLoader(JSObjectReference = true)]

Expand Down Expand Up @@ -126,8 +126,9 @@
}
else
{
<ErrorLogger EnableErrorLogger="@EnableLogger" ShowToast="@ShowToast" ToastTitle="@ErrorLoggerToastTitle"
EnableILogger="@EnableILogger" OnErrorHandleAsync="OnErrorHandleAsync" OnInitializedCallback="OnErrorLoggerInitialized">
<ErrorLogger EnableErrorLogger="@EnableLogger" EnableILogger="@EnableILogger"
ShowToast="@ShowToast" ToastTitle="@ErrorLoggerToastTitle"
OnErrorHandleAsync="OnErrorHandleAsync" OnInitializedCallback="OnErrorLoggerInitialized">
@HandlerMain()
</ErrorLogger>
}
Expand Down
18 changes: 9 additions & 9 deletions src/BootstrapBlazor/Components/Layout/Layout.razor.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -473,22 +473,22 @@ public partial class Layout : IHandlerException, ITabHeader
public object? Resource { get; set; }

/// <summary>
/// 获得/设置 是否开启全局异常捕获 默认 null 读取配置文件 EnableErrorLogger
/// 获得/设置 是否开启全局异常捕获 默认 null 使用 <see cref="BootstrapBlazorOptions.EnableErrorLogger"/> 设置值
/// </summary>
[Parameter]
public bool? EnableErrorLogger { get; set; }

/// <summary>
/// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 <see cref="BootstrapBlazorOptions.ShowErrorLoggerToast"/> 设置值
/// 获得/设置 是否记录异常到 <see cref="ILogger"/> 默认 null 使用 <see cref="BootstrapBlazorOptions.EnableErrorLoggerILogger"/> 设置值
/// </summary>
[Parameter]
public bool? ShowErrorLoggerToast { get; set; }
public bool? EnableErrorLoggerILogger { get; set; }

/// <summary>
/// 获得/设置 是否启用日志记录功能 默认 null 启用 使用 <see cref="BootstrapBlazorOptions.EnableErrorLoggerILogger"/> 设置值
/// 获得/设置 是否显示 Error 提示弹窗 默认 null 使用 <see cref="BootstrapBlazorOptions.ShowErrorLoggerToast"/> 设置值
/// </summary>
[Parameter]
public bool? EnableErrorLoggerILogger { get; set; }
public bool? ShowErrorLoggerToast { get; set; }

/// <summary>
/// 获得/设置 错误日志 <see cref="Toast"/> 弹窗标题 默认 null
Expand Down Expand Up @@ -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;

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down Expand Up @@ -702,7 +702,7 @@ private Task OnErrorLoggerInitialized(ErrorLogger logger)
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
public virtual Task HandlerException(Exception ex, RenderFragment<Exception> errorContent)
public virtual Task HandlerExceptionAsync(Exception ex, RenderFragment<Exception> errorContent)
{
_errorContent = errorContent(ex);
StateHasChanged();
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -453,7 +453,7 @@ private RenderFragment RenderBodyTemplate() => builder =>
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
public virtual Task HandlerException(Exception ex, RenderFragment<Exception> errorContent)
public virtual Task HandlerExceptionAsync(Exception ex, RenderFragment<Exception> errorContent)
{
_errorContent = errorContent(ex);
StateHasChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Tab/TabItemContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Render()
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
public Task HandlerException(Exception ex, RenderFragment<Exception> errorContent) => DialogService.ShowErrorHandlerDialog(errorContent(ex));
public Task HandlerExceptionAsync(Exception ex, RenderFragment<Exception> errorContent) => DialogService.ShowErrorHandlerDialog(errorContent(ex));

/// <summary>
/// IDispose 方法用于释放资源
Expand Down
3 changes: 0 additions & 3 deletions test/UnitTest/Components/BootstrapBlazorRootTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

//using HarmonyLib;

using Bunit.TestDoubles;

namespace UnitTest.Components;

public class BootstrapBlazorRootTest : TestBase
Expand All @@ -27,7 +25,6 @@ class MockGenerator : IRootComponentGenerator
/// <inheritdoc/>
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public RenderFragment Generator() => builder =>
{
builder.AddContent(0, new MarkupString("<div class=\"auto-generator\"></div>"));
Expand Down
24 changes: 16 additions & 8 deletions test/UnitTest/Components/ErrorLoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BootstrapBlazorRoot>(pb =>
{
Expand All @@ -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]
Expand Down Expand Up @@ -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<BootstrapBlazorRoot>(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) =>
{
Expand All @@ -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<DivideByZeroException>(async () => await cut.InvokeAsync(() => button.Click()));
}

[Fact]
public void ErrorContent_Ok()
public async Task ErrorContent_Ok()
{
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
{
pb.Add(a => a.EnableErrorLogger, true);
pb.Add(a => a.ShowToast, true);
pb.Add(a => a.ShowErrorLoggerToast, true);
pb.AddChildContent<Button>(pb =>
{
pb.Add(b => b.OnClick, () =>
Expand All @@ -184,7 +192,7 @@ public void ErrorContent_Ok()
}));
});
var button = cut.Find("button");
cut.InvokeAsync(() => button.Click());
await cut.InvokeAsync(() => button.Click());
cut.Contains("Attempted to divide by zero.error_content_template");
}

Expand Down
Loading
Loading