Skip to content

Commit

Permalink
feat(boot,infra): show error dialog on any fatal errors, not just one…
Browse files Browse the repository at this point in the history
…s thrown via `Bus`
  • Loading branch information
lars-berger committed Apr 4, 2022
1 parent 4f2a0b6 commit 476f025
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 67 deletions.
25 changes: 13 additions & 12 deletions GlazeWM.Bootstrapper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;

namespace GlazeWM.Bootstrapper
{
Expand All @@ -33,21 +32,23 @@ static void Main()
return;
}

// Set the process-default DPI awareness.
SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);

var serviceCollection = new ServiceCollection();
serviceCollection.AddInfrastructureServices();
serviceCollection.AddDomainServices();
serviceCollection.AddBarServices();
serviceCollection.AddSingleton<Startup>();

ServiceLocator.Provider = serviceCollection.BuildServiceProvider();
ServiceLocator.Provider = BuildServiceProvider();

var startup = ServiceLocator.Provider.GetRequiredService<Startup>();
startup.Init();
startup.Run();
Application.Run();
}
}

private static ServiceProvider BuildServiceProvider()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddInfrastructureServices();
serviceCollection.AddDomainServices();
serviceCollection.AddBarServices();
serviceCollection.AddSingleton<Startup>();

return serviceCollection.BuildServiceProvider();
}
}
}
50 changes: 34 additions & 16 deletions GlazeWM.Bootstrapper/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using GlazeWM.Infrastructure.WindowsApi;
using GlazeWM.Infrastructure.WindowsApi.Events;
using System;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Forms;
Expand Down Expand Up @@ -55,29 +56,46 @@ SystemEventService systemEventService
_systemEventService = systemEventService;
}

public void Init()
public void Run()
{
// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
_barService.StartApp();
try
{
// Set the process-default DPI awareness.
SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);

// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
_barService.StartApp();

// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();
// Populate initial monitors, windows, workspaces and user config.
PopulateInitialState();

// Listen on registered keybindings.
_keybindingService.Start();
// Listen on registered keybindings.
_keybindingService.Start();

// Listen for window events (eg. close, focus).
_windowEventService.Start();
// Listen for window events (eg. close, focus).
_windowEventService.Start();

// Listen for system-related events (eg. changes to display settings).
_systemEventService.Start();
// Listen for system-related events (eg. changes to display settings).
_systemEventService.Start();

// Add application to system tray.
_systemTrayService.AddToSystemTray();
// Add application to system tray.
_systemTrayService.AddToSystemTray();

_bus.Events.Where(@event => @event is ApplicationExitingEvent)
.Subscribe((@event) => OnApplicationExit());
_bus.Events.Where(@event => @event is ApplicationExitingEvent)
.Subscribe((@event) => OnApplicationExit());
}
catch (Exception error)
{
// Alert the user of the error.
// TODO: This throws duplicate errors if a command errors and it was invoked by another
// handler.
if (error is FatalUserException)
MessageBox.Show(error.Message);

File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");
throw error;
}
}

/// <summary>
Expand Down
54 changes: 15 additions & 39 deletions GlazeWM.Infrastructure/Bussing/Bus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reactive.Subjects;
using System.Windows;

namespace GlazeWM.Infrastructure.Bussing
{
Expand All @@ -21,28 +19,17 @@ public sealed class Bus
/// </summary>
public CommandResponse Invoke<T>(T command) where T : Command
{
Debug.WriteLine($"Command {command.Name} invoked.");

// Create a `Type` object representing the constructed `ICommandHandler` generic.
var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());

Debug.WriteLine($"Command {command.Name} invoked.");
var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType)
as ICommandHandler<T>;

try
lock (_lockObj)
{
var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType) as ICommandHandler<T>;
lock (_lockObj)
{
return handlerInstance.Handle(command);
}
}
catch (Exception error)
{
// Alert the user of the error.
// TODO: This throws duplicate errors if a command errors and it was invoked by another handler.
if (error is FatalUserException)
MessageBox.Show(error.Message);

File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");
throw error;
return handlerInstance.Handle(command);
}
}

Expand All @@ -51,35 +38,24 @@ public CommandResponse Invoke<T>(T command) where T : Command
/// </summary>
public void RaiseEvent<T>(T @event) where T : Event
{
Debug.WriteLine($"Event {@event.Name} emitted.");

// Create a `Type` object representing the constructed `IEventHandler` generic.
var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());

Debug.WriteLine($"Event {@event.Name} emitted.");
var handlerInstances = ServiceLocator.Provider.GetServices(handlerType)
as IEnumerable<IEventHandler<T>>;

try
foreach (var handler in handlerInstances)
{
var handlerInstances = ServiceLocator.Provider.GetServices(handlerType) as IEnumerable<IEventHandler<T>>;

foreach (var handler in handlerInstances)
lock (_lockObj)
{
lock (_lockObj)
{
handler.Handle(@event);
}
handler.Handle(@event);
}

// Emit event through subject.
Events.OnNext(@event);
}
catch (Exception error)
{
// Alert the user of the error.
if (error is FatalUserException)
MessageBox.Show(error.Message);

File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");
throw error;
}
// Emit event through subject.
Events.OnNext(@event);
}
}
}

0 comments on commit 476f025

Please sign in to comment.