GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: exception handling in async bus methods (#154)
lars-bergerabb8f2a845db
diff --git a/GlazeWM.Bar/BarService.cs b/GlazeWM.Bar/BarService.csindex 4f30e499..5e2f1e64 100644--- a/GlazeWM.Bar/BarService.cs+++ b/GlazeWM.Bar/BarService.cs@@ -2,7 +2,7 @@ using GlazeWM.Domain.Monitors;using GlazeWM.Domain.Monitors.Events;using GlazeWM.Domain.UserConfigs.Events;using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Exceptions;+using GlazeWM.Infrastructure.Common.Commands;using System.Reactive.Linq;using System;using System.Threading;@@ -17,15 +17,13 @@ namespace GlazeWM.Bar{private readonly Bus _bus;private readonly MonitorService _monitorService;- private readonly ExceptionHandler _exceptionHandler;private Application _application;private readonly Dictionary<string, MainWindow> _activeWindowsByDeviceName = new();- public BarService(Bus bus, MonitorService monitorService, ExceptionHandler exceptionHandler)+ public BarService(Bus bus, MonitorService monitorService){_bus = bus;_monitorService = monitorService;- _exceptionHandler = exceptionHandler;}public void StartApp()@@ -53,7 +51,7 @@ namespace GlazeWM.Bar}catch (Exception exception){- _exceptionHandler.HandleFatalException(exception);+ _bus.Invoke(new HandleFatalExceptionCommand(exception));}}){diff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.csindex 1fcd507d..4c35bab0 100644--- a/GlazeWM.Bootstrapper/Program.cs+++ b/GlazeWM.Bootstrapper/Program.cs@@ -68,7 +68,7 @@ namespace GlazeWM.Bootstrapper// Configure exception handler.services- .AddOptions<ExceptionHandlerOptions>()+ .AddOptions<ExceptionHandlingOptions>().Configure<ContainerService, JsonService>((options, containerService, jsonService) =>{options.ErrorLogPath = Path.Combine(diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.csindex 3cb2852a..9491f39e 100644--- a/GlazeWM.Bootstrapper/Startup.cs+++ b/GlazeWM.Bootstrapper/Startup.cs@@ -5,7 +5,6 @@ using GlazeWM.Domain.Windows.Commands;using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.Common.Commands;using GlazeWM.Infrastructure.Common.Events;-using GlazeWM.Infrastructure.Exceptions;using GlazeWM.Infrastructure.WindowsApi;using System;using System.Collections.Generic;@@ -20,7 +19,6 @@ namespace GlazeWM.Bootstrapper{private readonly BarService _barService;private readonly Bus _bus;- private readonly ExceptionHandler _exceptionHandler;private readonly KeybindingService _keybindingService;private readonly SystemEventService _systemEventService;private readonly WindowEventService _windowEventService;@@ -30,14 +28,12 @@ namespace GlazeWM.Bootstrapperpublic Startup(BarService barService,Bus bus,- ExceptionHandler exceptionHandler,KeybindingService keybindingService,SystemEventService systemEventService,WindowEventService windowEventService){_barService = barService;_bus = bus;- _exceptionHandler = exceptionHandler;_keybindingService = keybindingService;_systemEventService = systemEventService;_windowEventService = windowEventService;@@ -88,7 +84,7 @@ namespace GlazeWM.Bootstrapper}catch (Exception exception){- _exceptionHandler.HandleFatalException(exception);+ _bus.Invoke(new HandleFatalExceptionCommand(exception));}}diff --git a/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs b/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.csindex 0d5c80e1..cdb34963 100644--- a/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs+++ b/GlazeWM.Domain/Common/CommandHandlers/ExecProcessHandler.cs@@ -1,8 +1,8 @@using System;using System.Diagnostics;+using System.Windows.Forms;using GlazeWM.Domain.Common.Commands;using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Exceptions;namespace GlazeWM.Domain.Common.CommandHandlers{@@ -30,8 +30,9 @@ namespace GlazeWM.Domain.Common.CommandHandlers}catch (Exception exception){+ // Alert the user of the error.// TODO: Link to documentation for `exec` command (no proper documentation yet).- ExceptionHandler.HandleNonFatalException(exception);+ MessageBox.Show(exception.Message);}return CommandResponse.Ok;diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.csindex cb63e916..38c73518 100644--- a/GlazeWM.Infrastructure/Bussing/Bus.cs+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs@@ -1,4 +1,6 @@-using Microsoft.Extensions.Logging;+using GlazeWM.Infrastructure.Common.Commands;+using Microsoft.Extensions.Logging;+using System;using System.Collections.Generic;using System.Reactive.Subjects;using System.Threading.Tasks;@@ -40,7 +42,18 @@ namespace GlazeWM.Infrastructure.Bussingpublic Task<CommandResponse> InvokeAsync<T>(T command) where T : Command{- return Task.Run(() => Invoke(command));+ return Task.Run(() =>+ {+ try+ {+ return Invoke(command);+ }+ catch (Exception e)+ {+ Invoke(new HandleFatalExceptionCommand(e));+ throw;+ }+ });}/// <summary>@@ -68,7 +81,17 @@ namespace GlazeWM.Infrastructure.Bussingpublic Task EmitAsync<T>(T @event) where T : Event{- return Task.Run(() => Emit(@event));+ return Task.Run(() =>+ {+ try+ {+ Emit(@event);+ }+ catch (Exception e)+ {+ Invoke(new HandleFatalExceptionCommand(e));+ }+ });}}}diff --git a/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.csnew file mode 100644index 00000000..4587a565--- /dev/null+++ b/GlazeWM.Infrastructure/Common/CommandHandlers/HandleFatalExceptionHandler.cs@@ -0,0 +1,48 @@+using System;+using System.IO;+using System.Windows.Forms;+using GlazeWM.Infrastructure.Bussing;+using GlazeWM.Infrastructure.Common.Commands;+using GlazeWM.Infrastructure.Exceptions;+using Microsoft.Extensions.Options;++namespace GlazeWM.Infrastructure.Common.CommandHandlers+{+ internal class HandleFatalExceptionHandler : ICommandHandler<HandleFatalExceptionCommand>+ {+ private readonly Bus _bus;+ private readonly IOptions<ExceptionHandlingOptions> _options;++ public HandleFatalExceptionHandler(Bus bus, IOptions<ExceptionHandlingOptions> options)+ {+ _bus = bus;+ _options = options;+ }++ public CommandResponse Handle(HandleFatalExceptionCommand command)+ {+ // Alert the user of the error.+ var exception = command.Exception;+ MessageBox.Show($"Unhandled exception: {exception.Message}");++ WriteToErrorLog(exception);++ _bus.Invoke(new ExitApplicationCommand(true));++ return CommandResponse.Ok;+ }++ private void WriteToErrorLog(Exception exception)+ {+ var errorLogPath = _options.Value.ErrorLogPath;++ // Create containing directory. Needs to be created before writing to the file.+ Directory.CreateDirectory(Path.GetDirectoryName(errorLogPath));++ File.AppendAllText(+ errorLogPath,+ _options.Value.ErrorLogMessageDelegate(exception)+ );+ }+ }+}diff --git a/GlazeWM.Infrastructure/Common/Commands/HandleFatalExceptionCommand.cs b/GlazeWM.Infrastructure/Common/Commands/HandleFatalExceptionCommand.csnew file mode 100644index 00000000..6dab324b--- /dev/null+++ b/GlazeWM.Infrastructure/Common/Commands/HandleFatalExceptionCommand.cs@@ -0,0 +1,15 @@+using System;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Infrastructure.Common.Commands+{+ public class HandleFatalExceptionCommand : Command+ {+ public Exception Exception { get; }++ public HandleFatalExceptionCommand(Exception exception)+ {+ Exception = exception;+ }+ }+}diff --git a/GlazeWM.Infrastructure/DependencyInjection.cs b/GlazeWM.Infrastructure/DependencyInjection.csindex 74d58faf..0b5b994f 100644--- a/GlazeWM.Infrastructure/DependencyInjection.cs+++ b/GlazeWM.Infrastructure/DependencyInjection.cs@@ -13,7 +13,6 @@ namespace GlazeWM.Infrastructurepublic static IServiceCollection AddInfrastructureServices(this IServiceCollection services){services.AddSingleton<Bus>();- services.AddSingleton<ExceptionHandler>();services.AddSingleton<KeybindingService>();services.AddSingleton<SystemEventService>();services.AddSingleton<WindowEventService>();@@ -21,6 +20,7 @@ namespace GlazeWM.Infrastructureservices.AddSingleton<YamlService>();services.AddSingleton<ICommandHandler<ExitApplicationCommand>, ExitApplicationHandler>();+ services.AddSingleton<ICommandHandler<HandleFatalExceptionCommand>, HandleFatalExceptionHandler>();services.AddSingleton<ICommandHandler<NoopCommand>, NoopHandler>();// TODO: Change WindowsApiService to be compatible with DI.diff --git a/GlazeWM.Infrastructure/Exceptions/ExceptionHandler.cs b/GlazeWM.Infrastructure/Exceptions/ExceptionHandler.csdeleted file mode 100644index 2c1fe3d9..00000000--- a/GlazeWM.Infrastructure/Exceptions/ExceptionHandler.cs+++ /dev/null@@ -1,54 +0,0 @@-using System;-using System.IO;-using System.Windows.Forms;-using GlazeWM.Infrastructure.Bussing;-using GlazeWM.Infrastructure.Common.Commands;-using Microsoft.Extensions.Options;--namespace GlazeWM.Infrastructure.Exceptions-{- public class ExceptionHandler- {- private readonly Bus _bus;- private readonly IOptions<ExceptionHandlerOptions> _options;-- public ExceptionHandler(Bus bus, IOptions<ExceptionHandlerOptions> options)- {- _bus = bus;- _options = options;- }-- public static void HandleNonFatalException(Exception exception)- {- // Alert the user of the error.- MessageBox.Show(exception.Message);- }-- public void HandleFatalException(Exception exception)- {- // Lock bus to prevent further state changes while showing message box and writing to logs.- lock (_bus.LockObj)- {- // Alert the user of the error.- MessageBox.Show($"Unhandled exception: {exception.Message}");-- WriteToErrorLog(exception);-- _bus.Invoke(new ExitApplicationCommand(true));- }- }-- private void WriteToErrorLog(Exception exception)- {- var errorLogPath = _options.Value.ErrorLogPath;-- // Create containing directory. Needs to be created before writing to the file.- Directory.CreateDirectory(Path.GetDirectoryName(errorLogPath));-- File.AppendAllText(- errorLogPath,- _options.Value.ErrorLogMessageDelegate(exception)- );- }- }-}diff --git a/GlazeWM.Infrastructure/Exceptions/ExceptionHandlerOptions.cs b/GlazeWM.Infrastructure/Exceptions/ExceptionHandlingOptions.cssimilarity index 82%rename from GlazeWM.Infrastructure/Exceptions/ExceptionHandlerOptions.csrename to GlazeWM.Infrastructure/Exceptions/ExceptionHandlingOptions.csindex ed9239b9..e888fea0 100644--- a/GlazeWM.Infrastructure/Exceptions/ExceptionHandlerOptions.cs+++ b/GlazeWM.Infrastructure/Exceptions/ExceptionHandlingOptions.cs@@ -2,7 +2,7 @@ using System;namespace GlazeWM.Infrastructure.Exceptions{- public class ExceptionHandlerOptions+ public class ExceptionHandlingOptions{public string ErrorLogPath { get; set; }public Func<Exception, string> ErrorLogMessageDelegate { get; set; }