Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -5,6 +5,7 @@

using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Reflection;

Expand All @@ -31,6 +32,10 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
[NotNull]
private NavigationManager? NavigationManager { get; set; }

[Inject]
[NotNull]
private IHostEnvironment? IHostEnvironment { get; set; }
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated

/// <summary>
/// 获得/设置 自定义错误处理回调方法
/// </summary>
Expand All @@ -54,13 +59,10 @@ class BootstrapBlazorErrorBoundary : ErrorBoundaryBase
/// <inheritdoc/>
/// </summary>
/// <param name="exception"></param>
protected override async Task OnErrorAsync(Exception exception)
protected override Task OnErrorAsync(Exception exception)
{
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);
return Task.CompletedTask;
}

/// <summary>
Expand All @@ -69,13 +71,13 @@ protected override async Task OnErrorAsync(Exception exception)
/// <param name="builder"></param>
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
// 页面生命周期内异常直接调用这里
var ex = CurrentException ?? _exception;
if (ex != null)
{
// 处理自定义异常逻辑
if (OnErrorHandleAsync != null)
{
// 页面生命周期内异常直接调用这里
_ = OnErrorHandleAsync(Logger, ex);
return;
}
Expand Down Expand Up @@ -132,7 +134,7 @@ private MarkupString GetErrorContentMarkupString(Exception ex)
}

/// <summary>
/// 渲染异常信息方法
/// BootstrapBlazor 组件导致异常渲染方法
/// </summary>
/// <param name="exception"></param>
/// <param name="handler"></param>
Expand All @@ -145,14 +147,35 @@ public async Task RenderException(Exception exception, IHandlerException? handle
return;
}

// 记录日志
await OnErrorAsync(exception);

if (handler != null)
{
await handler.HandlerException(exception, ExceptionContent);
if (IHostEnvironment.IsDevelopment())
{
// IHandlerException 处理异常逻辑
await handler.HandlerException(exception, ExceptionContent);
}
else
{
// 非开发模式下弹窗提示错误信息
await ToastService.Error(ToastTitle, exception.Message);
}
return;
}

await OnErrorAsync(exception);
// 显示异常信息
await ShowErrorToast(exception);
_exception = exception;
StateHasChanged();
}

private async Task ShowErrorToast(Exception exception)
{
if (ShowToast)
{
await ToastService.Error(ToastTitle, exception.Message);
}
}
}
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Layout/Layout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ private Task OnErrorLoggerInitialized(ErrorLogger logger)
private RenderFragment? _errorContent;

/// <summary>
/// HandlerException 错误处理方法
/// <inheritdoc/>
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Modal/ModalDialog.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ private RenderFragment RenderBodyTemplate() => builder =>
protected RenderFragment? _errorContent;

/// <summary>
/// HandlerException 错误处理方法
/// <inheritdoc/>
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
Expand Down
7 changes: 5 additions & 2 deletions src/BootstrapBlazor/Components/Tab/TabItemContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ public void Render()
}

/// <summary>
/// HandlerException 错误处理方法
/// <inheritdoc/>
/// </summary>
/// <param name="ex"></param>
/// <param name="errorContent"></param>
public Task HandlerException(Exception ex, RenderFragment<Exception> errorContent) => DialogService.ShowErrorHandlerDialog(errorContent(ex));
public async Task HandlerException(Exception ex, RenderFragment<Exception> errorContent)
{
await DialogService.ShowErrorHandlerDialog(errorContent(ex));
}
Comment thread
ArgoZhang marked this conversation as resolved.
Outdated

/// <summary>
/// IDispose 方法用于释放资源
Expand Down
4 changes: 4 additions & 0 deletions src/BootstrapBlazor/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET6Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(NET8Version)" />
</ItemGroup>

Expand All @@ -44,6 +45,7 @@
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET7Version)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(NET8Version)" />
</ItemGroup>

Expand All @@ -53,6 +55,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(NET8Version)" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET8Version)" />
</ItemGroup>

Expand All @@ -62,6 +65,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Http" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(NET9Version)" />
<PackageReference Include="Microsoft.Extensions.Localization" Version="$(NET9Version)" />
</ItemGroup>

Expand Down
Loading