GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: improvements to fatal exception handling (#258)
lars-bergera3de8c07b7b9
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.csindex ebd239ad..737ccc49 100644--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs@@ -1,9 +1,11 @@+using System;using System.Linq;using System.Threading.Tasks;using GlazeWM.Domain.Containers;using GlazeWM.Domain.UserConfigs.Commands;using GlazeWM.Domain.Windows;using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.Common.Commands;using GlazeWM.Infrastructure.WindowsApi;using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;@@ -48,32 +50,40 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers{Task.Run(() =>{- // Avoid invoking keybinding if an ignored window currently has focus.- if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))- return;-- var subjectContainer = _containerService.FocusedContainer;- var subjectContainerId = subjectContainer.Id;-- // Invoke commands in sequence on keybinding press.- foreach (var commandString in formattedCommandStrings)+ try{- // Avoid calling command if container gets detached. This is to prevent crashes- // for edge cases like ["close", "move to workspace X"].- if (subjectContainer.IsDetached())+ // Avoid invoking keybinding if an ignored window currently has focus.+ if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))return;- var parsedCommand = _commandParsingService.ParseCommand(- commandString,- subjectContainer- );+ var subjectContainer = _containerService.FocusedContainer;+ var subjectContainerId = subjectContainer.Id;++ // Invoke commands in sequence on keybinding press.+ foreach (var commandString in formattedCommandStrings)+ {+ // Avoid calling command if container gets detached. This is to prevent crashes+ // for edge cases like ["close", "move to workspace X"].+ if (subjectContainer.IsDetached())+ return;- // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.- _bus.Invoke((dynamic)parsedCommand);+ var parsedCommand = _commandParsingService.ParseCommand(+ commandString,+ subjectContainer+ );- // Update subject container in case the reference changes (eg. when going from a tiling to a- // floating window).- subjectContainer = _containerService.GetContainerById(subjectContainerId);+ // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.+ _bus.Invoke((dynamic)parsedCommand);++ // Update subject container in case the reference changes (eg. when going from a tiling to a+ // floating window).+ subjectContainer = _containerService.GetContainerById(subjectContainerId);+ }+ }+ catch (Exception e)+ {+ _bus.Invoke(new HandleFatalExceptionCommand(e));+ throw;}});});diff --git a/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.csindex 8afe4d99..b4166040 100644--- a/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs+++ b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs@@ -21,14 +21,31 @@ namespace GlazeWM.Infrastructure.Common.CommandHandlerspublic CommandResponse Handle(HandleFatalExceptionCommand command){- // Alert the user of the error.var exception = command.Exception;- // TODO: Show foreground window.- MessageBox.Show($"Unhandled exception: {exception.Message}");++ var dialogButtons = exception is FatalUserException+ ? MessageBoxButtons.OK+ : MessageBoxButtons.OKCancel;++ var dialogMesssage = exception is FatalUserException+ ? exception.Message+ : $"Unhandled exception: {exception.Message}\n\nContinue without exiting?";++ // Alert the user of the error.+ var dialogResult = MessageBox.Show(+ dialogMesssage,+ "Encountered a problem",+ dialogButtons,+ MessageBoxIcon.Warning,+ MessageBoxDefaultButton.Button1,+ MessageBoxOptions.DefaultDesktopOnly+ );WriteToErrorLog(exception);- _bus.Invoke(new ExitApplicationCommand(true));+ // Always exit on `FatalUserException`.+ if (dialogResult == DialogResult.Cancel || exception is FatalUserException)+ _bus.Invoke(new ExitApplicationCommand(true));return CommandResponse.Ok;}