From 32569e3da3e2b584beb37bd186857fdb57df1811 Mon Sep 17 00:00:00 2001 From: ahmdkaml Date: Fri, 9 Oct 2026 06:26:17 +0300 Subject: [PATCH 1/2] implementing catch-exceptions --- src/TextForge.Desktop/App.axaml | 18 +++++-- src/TextForge.Desktop/App.axaml.cs | 22 +++++++-- src/TextForge.Desktop/Program.cs | 42 ++++++++++++++-- src/TextForge.Desktop/Views/MainWindow.axaml | 51 +++++++++++++------- 4 files changed, 104 insertions(+), 29 deletions(-) diff --git a/src/TextForge.Desktop/App.axaml b/src/TextForge.Desktop/App.axaml index e0c541b..521af53 100644 --- a/src/TextForge.Desktop/App.axaml +++ b/src/TextForge.Desktop/App.axaml @@ -13,11 +13,19 @@ - 4 - 8 - 10 - 12 - 16 + + 4 + 8 + 10 + 12 + 16 + + + 4 + 8 + 10 + 12 + 16 8 8,4 diff --git a/src/TextForge.Desktop/App.axaml.cs b/src/TextForge.Desktop/App.axaml.cs index 4d6ae73..19aa89f 100644 --- a/src/TextForge.Desktop/App.axaml.cs +++ b/src/TextForge.Desktop/App.axaml.cs @@ -16,16 +16,26 @@ public partial class App : Application public override void Initialize() { - AvaloniaXamlLoader.Load(this); + try + { + AvaloniaXamlLoader.Load(this); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[App.Initialize] Failed to load App.axaml:\n{ex}"); + throw; + } } public override void OnFrameworkInitializationCompleted() { - var collection = new ServiceCollection(); + try + { + var collection = new ServiceCollection(); collection.AddSingleton(); collection.AddTransient(); collection.AddTransient(); - + Services = collection.BuildServiceProvider(); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) @@ -36,5 +46,11 @@ public override void OnFrameworkInitializationCompleted() } base.OnFrameworkInitializationCompleted(); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[App.OnFrameworkInitializationCompleted] Failed to initialize application:\n{ex}"); + throw; + } } } diff --git a/src/TextForge.Desktop/Program.cs b/src/TextForge.Desktop/Program.cs index ce04c81..b1ec285 100644 --- a/src/TextForge.Desktop/Program.cs +++ b/src/TextForge.Desktop/Program.cs @@ -5,20 +5,54 @@ namespace TextForge.Desktop; sealed class Program { + // Initialization code. Don't use any Avalonia, third-party APIs or any // SynchronizationContext-reliant code before AppMain is called: things aren't initialized // yet and stuff might break. [STAThread] - public static void Main(string[] args) => BuildAvaloniaApp() - .StartWithClassicDesktopLifetime(args); + public static void Main(string[] args) + { + try + { + BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); + } + catch (Exception ex) + { + Console.Error.WriteLine($"[Startup Crash]: {ex}"); + LogFatalException("Startup Crash in BuildAvaloniaApp", ex); + throw; + } + AppDomain.CurrentDomain.UnhandledException += (sender, e) => + { + LogFatalException("AppDomain.CurrentDomain.UnhandledException", e.ExceptionObject as Exception); + }; + System.Threading.Tasks.TaskScheduler.UnobservedTaskException += (sender, e) => + { + LogFatalException("TaskScheduler.UnobservedTaskException", e.Exception); + e.SetObserved(); + }; + } + private static void LogFatalException(string source, Exception? ex) + { + string message = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] FATAL ERROR in {source}:\n{ex}\n\n"; + Console.Error.WriteLine(message); + try + { + System.IO.File.AppendAllText("crash.log", message); + } + catch + { + // Fail silently if disk write is prohibited + } + } // Avalonia configuration, don't remove; also used by visual designer. public static AppBuilder BuildAvaloniaApp() => AppBuilder.Configure() .UsePlatformDetect() -#if DEBUG + #if DEBUG .WithDeveloperTools() -#endif + #endif .WithInterFont() .LogToTrace(); } diff --git a/src/TextForge.Desktop/Views/MainWindow.axaml b/src/TextForge.Desktop/Views/MainWindow.axaml index cd14df5..944bb2a 100644 --- a/src/TextForge.Desktop/Views/MainWindow.axaml +++ b/src/TextForge.Desktop/Views/MainWindow.axaml @@ -6,39 +6,56 @@ x:CompileBindings="True" Title="TextForge" Width="1024" Height="768" - Focusable="True" - > + Focusable="True"> + - - + + - + - + + - + - + + + - + - + + + - + - - - - - - + + + + From d7c9fef1d9dc68f4902ee66a889d8d001a948a54 Mon Sep 17 00:00:00 2001 From: ahmdkaml Date: Fri, 9 Oct 2026 06:27:52 +0300 Subject: [PATCH 2/2] implementing catch-exceptions --- scripts/implement.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/implement.ps1 b/scripts/implement.ps1 index a23e2cb..d0dbc3f 100644 --- a/scripts/implement.ps1 +++ b/scripts/implement.ps1 @@ -93,7 +93,7 @@ Write-Host "Creating pull request..." Run-Gh @( "pr", "create", - "--base", "ui-rewrite", + "--base", "main", "--head", $BranchName, "--title", "implementing $BranchName [$IssueNumber]", "--body", "closes #$IssueNumber" @@ -114,18 +114,18 @@ Run-Gh @( ) # -------------------------------------------------- -# 8. Switch back to ui-rewrite +# 8. Switch back to main # -------------------------------------------------- -Write-Host "Switching to ui-rewrite..." -Run-Git @("switch", "ui-rewrite") +Write-Host "Switching to main..." +Run-Git @("switch", "main") # -------------------------------------------------- # 9. Get latest changes # -------------------------------------------------- -Write-Host "Pulling latest ui-rewrite..." -Run-Git @("pull", "origin", "ui-rewrite") +Write-Host "Pulling latest main..." +Run-Git @("pull", "origin", "main") # -------------------------------------------------- # 10. Delete local feature branch @@ -134,4 +134,4 @@ Run-Git @("pull", "origin", "ui-rewrite") Write-Host "" Write-Host "Successfully implemented issue #$IssueNumber." -ForegroundColor Green Write-Host "Branch: $BranchName" -Write-Host "PR merged into: ui-rewrite" +Write-Host "PR merged into: main"