Skip to content

Commit

Permalink
chore: Added more error handling to Auto Updater application
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorixon committed Aug 2, 2024
1 parent f079bf2 commit 05b3339
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
59 changes: 52 additions & 7 deletions src/JASM.AutoUpdater/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using Microsoft.UI.Xaml;
using Serilog;
using WinUIEx;
Expand All @@ -24,16 +29,21 @@ public App()
InitializeComponent();


var logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Information()
.WriteTo.File("logs\\auto-updater-log.txt")
.WriteTo.Console()
.CreateLogger();
var logger = CreateLogger();

Log.Logger = logger;
}

private static ILogger CreateLogger() => new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Information()
.WriteTo.File(LogFilePath)
.WriteTo.Console()
.CreateLogger();

private static readonly string LogFilePath =
$@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\logs\auto-updater-log.txt";

/// <summary>
/// Invoked when the application is launched.
/// </summary>
Expand Down Expand Up @@ -61,7 +71,42 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Log.Error(e.Exception, "Unhandled exception");
Log.Information("------");
Log.CloseAndFlush();

e.Handled = true;

var result = PInvoke.MessageBox(
new HWND(WinRT.Interop.WindowNative.GetWindowHandle(MainWindow)),
$"Check the logs file for more info: {LogFilePath}\n\n" +
$"Press Yes to close the program. Press No to ignore the error and continue",
$"An error occured: {e.Exception.GetType()} | {e.Exception.Message ?? "null"}",
MESSAGEBOX_STYLE.MB_ICONSTOP | MESSAGEBOX_STYLE.MB_YESNO | MESSAGEBOX_STYLE.MB_APPLMODAL |
MESSAGEBOX_STYLE.MB_SETFOREGROUND);

if (result == MESSAGEBOX_RESULT.IDYES)
{
Current.Exit();
return;
}

Log.Logger = CreateLogger();

var mainPage = MainWindow.Content as MainPage;
MainWindow.DispatcherQueue.TryEnqueue(() =>
{
try
{
mainPage?.ViewModel?.Stop("An unknown error occured..");
}
catch (Exception exception)
{
return;
}
});

return;
}

internal static MainWindow MainWindow;
internal static MainWindow MainWindow = null!;
}
8 changes: 8 additions & 0 deletions src/JASM.AutoUpdater/JASM.AutoUpdater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
<None Remove="Assets\7z\7z.dll" />
<None Remove="Assets\7z\7z.exe" />
<None Remove="MainPage.xaml" />
<None Remove="NativeMethods.txt" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="NativeMethods.txt" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.Segmented" Version="8.0.240109" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240607001" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down
2 changes: 1 addition & 1 deletion src/JASM.AutoUpdater/MainPageVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void Finish()
return release;
}

private void Stop(string stopReason)
public void Stop(string stopReason)
{
Stopped = true;
StopReason = stopReason;
Expand Down
1 change: 1 addition & 0 deletions src/JASM.AutoUpdater/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MessageBox

0 comments on commit 05b3339

Please sign in to comment.