Skip to content

Commit

Permalink
feat(templates): improve Boilerplate unobserved exception logging #9485
Browse files Browse the repository at this point in the history
… (#9486)
  • Loading branch information
ysmoradi authored Dec 14, 2024
1 parent 7928d69 commit c58c145
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ namespace Boilerplate.Client.Core.Services;

public abstract partial class ClientExceptionHandlerBase : SharedExceptionHandler, IExceptionHandler
{
[AutoInject] protected Bit.Butil.Console Console = default!;
[AutoInject] protected ITelemetryContext TelemetryContext = default!;
[AutoInject] protected readonly SnackBarService SnackBarService = default!;
[AutoInject] protected ILogger<ClientExceptionHandlerBase> Logger = default!;
[AutoInject] protected readonly ITelemetryContext TelemetryContext = default!;
[AutoInject] protected readonly MessageBoxService MessageBoxService = default!;
[AutoInject] protected readonly ILogger<ClientExceptionHandlerBase> Logger = default!;

public void Handle(Exception exception,
bool nonInterrupting = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ public static MauiApp CreateMauiApp()
AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject, reportedBy: nameof(AppDomain.UnhandledException));
TaskScheduler.UnobservedTaskException += (_, e) =>
{
if (LogException(e.Exception, nameof(TaskScheduler.UnobservedTaskException)))
{
e.SetObserved();
}
LogException(e.Exception, nameof(TaskScheduler.UnobservedTaskException));
e.SetObserved();
};

AppPlatform.IsBlazorHybrid = true;
Expand Down Expand Up @@ -196,17 +194,18 @@ public override void DecidePolicy(WKWebView webView, WKNavigationAction navigati
}
#endif

private static bool LogException(object? error, string reportedBy)
private static void LogException(object? error, string reportedBy)
{
var errorMessage = error?.ToString() ?? "Unknown error";
if (IPlatformApplication.Current?.Services is IServiceProvider services && error is Exception exp)
{
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
});
return true;
}, nonInterrupting: true);
}
else
{
_ = Console.Error.WriteLineAsync(error?.ToString() ?? "Unknown error");
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public static async Task Main(string[] args)

var host = builder.Build();

AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject, reportedBy: nameof(AppDomain.UnhandledException), host);
TaskScheduler.UnobservedTaskException += (_, e) =>
{
LogException(e.Exception, nameof(TaskScheduler.UnobservedTaskException), host);
e.SetObserved();
};

if (CultureInfoManager.MultilingualEnabled)
{
var cultureCookie = await host.Services.GetRequiredService<Cookie>().GetValue(".AspNetCore.Culture");
Expand All @@ -65,4 +72,19 @@ public static async Task Main(string[] args)

await host.RunAsync();
}

private static void LogException(object? error, string reportedBy, WebAssemblyHost host)
{
if (host.Services is IServiceProvider services && error is Exception exp)
{
services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}
else
{
_ = System.Console.Error.WriteLineAsync(error?.ToString() ?? "Unknown error");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ public static void Main(string[] args)

private static void LogException(object? error, string reportedBy)
{
var errorMessage = error?.ToString() ?? "Unknown error";
if (Services is not null && error is Exception exp)
{
Services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
Services.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
});
}, nonInterrupting: true);
}
else
{
var errorMessage = error?.ToString() ?? "Unknown error";
Clipboard.SetText(errorMessage);
System.Windows.Forms.MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//#if (api == "Integrated")
using Boilerplate.Server.Api.Data;
//#endif
using Boilerplate.Client.Core.Services.Contracts;

namespace Boilerplate.Server.Web;

Expand Down Expand Up @@ -35,6 +36,9 @@ public static async Task Main(string[] args)

var app = builder.Build();

AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject, reportedBy: nameof(AppDomain.UnhandledException), app);
TaskScheduler.UnobservedTaskException += (_, e) => { LogException(e.Exception, reportedBy: nameof(TaskScheduler.UnobservedTaskException), app); e.SetObserved(); };

//#if (api == "Integrated")
if (builder.Environment.IsDevelopment())
{
Expand All @@ -48,4 +52,20 @@ public static async Task Main(string[] args)

await app.RunAsync();
}

private static void LogException(object? error, string reportedBy, WebApplication app)
{
if (error is Exception exp)
{
using var scope = app.Services.CreateScope();
scope.ServiceProvider.GetRequiredService<IExceptionHandler>().Handle(exp, parameters: new()
{
{ nameof(reportedBy), reportedBy }
}, nonInterrupting: true);
}
else
{
_ = Console.Error.WriteLineAsync(error?.ToString() ?? "Unknown error");
}
}
}

0 comments on commit c58c145

Please sign in to comment.