Skip to content

Commit 9b1eee2

Browse files
committed
Updated other calls to InvokeAsync to be wrapped with an exception handler
1 parent cb195ec commit 9b1eee2

9 files changed

Lines changed: 148 additions & 25 deletions

File tree

src/EventLogExpert.UI.Tests/Services/SettingsServiceTests.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public void Load_WhenCalled_ShouldInvokeLoadedEvent()
247247
var settingsService = CreateSettingsService();
248248

249249
var eventInvoked = false;
250-
settingsService.Loaded = () => eventInvoked = true;
250+
settingsService.Loaded += () => eventInvoked = true;
251251

252252
// Act
253253
settingsService.Load();
@@ -257,14 +257,13 @@ public void Load_WhenCalled_ShouldInvokeLoadedEvent()
257257
}
258258

259259
[Fact]
260-
public void Load_WhenLoadedIsNull_ShouldNotThrow()
260+
public void Load_WhenNoSubscribers_ShouldNotThrow()
261261
{
262262
// Arrange
263263
var settingsService = CreateSettingsService();
264-
settingsService.Loaded = null;
265264

266-
// Act
267-
var exception = Record.Exception(() => settingsService.Load());
265+
// Act - no subscribers attached
266+
var exception = Record.Exception(settingsService.Load);
268267

269268
// Assert
270269
Assert.Null(exception);

src/EventLogExpert.UI/Interfaces/ISettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface ISettingsService
1313

1414
bool IsPreReleaseEnabled { get; set; }
1515

16-
Action? Loaded { get; set; }
16+
event Action? Loaded;
1717

1818
LogLevel LogLevel { get; set; }
1919

src/EventLogExpert.UI/Services/SettingsService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public bool IsPreReleaseEnabled
5353
}
5454
}
5555

56-
public Action? Loaded { get; set; }
56+
public event Action? Loaded;
5757

5858
public LogLevel LogLevel
5959
{

src/EventLogExpert/Components/EventTable.razor.cs

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Fluxor;
1414
using Microsoft.AspNetCore.Components;
1515
using Microsoft.AspNetCore.Components.Web;
16+
using Microsoft.Extensions.Logging;
1617
using Microsoft.JSInterop;
1718
using System.Collections.Immutable;
1819
using IDispatcher = Fluxor.IDispatcher;
@@ -43,14 +44,16 @@ public sealed partial class EventTable
4344

4445
[Inject] private ISettingsService Settings { get; init; } = null!;
4546

47+
[Inject] private IFileLogger TraceLogger { get; init; } = null!;
48+
4649
protected override async Task OnInitializedAsync()
4750
{
4851
SelectedEventState.Select(s => s.SelectedEvents);
4952

50-
SubscribeToAction<EventTableAction.SetActiveTable>(action => InvokeAsync(ScrollToSelectedEvent));
51-
SubscribeToAction<EventTableAction.LoadColumnsCompleted>(action => InvokeAsync(RegisterTableEventHandlers));
52-
SubscribeToAction<EventTableAction.UpdateCombinedEvents>(action => InvokeAsync(ScrollToSelectedEvent));
53-
SubscribeToAction<EventTableAction.UpdateDisplayedEvents>(action => InvokeAsync(ScrollToSelectedEvent));
53+
SubscribeToAction<EventTableAction.SetActiveTable>(OnSetActiveTable);
54+
SubscribeToAction<EventTableAction.LoadColumnsCompleted>(OnLoadColumnsCompleted);
55+
SubscribeToAction<EventTableAction.UpdateCombinedEvents>(OnUpdateCombinedEvents);
56+
SubscribeToAction<EventTableAction.UpdateDisplayedEvents>(OnUpdateDisplayedEvents);
5457

5558
_eventTableState = EventTableState.Value;
5659

@@ -156,6 +159,54 @@ private async Task InvokeContextMenu(MouseEventArgs args) =>
156159
private async Task InvokeTableColumnMenu(MouseEventArgs args) =>
157160
await JSRuntime.InvokeVoidAsync("invokeTableColumnMenu", args.ClientX, args.ClientY);
158161

162+
private async void OnLoadColumnsCompleted(EventTableAction.LoadColumnsCompleted action)
163+
{
164+
try
165+
{
166+
await InvokeAsync(RegisterTableEventHandlers);
167+
}
168+
catch (Exception e)
169+
{
170+
TraceLogger.Trace($"Failed to register table event handlers: {e}", LogLevel.Error);
171+
}
172+
}
173+
174+
private async void OnSetActiveTable(EventTableAction.SetActiveTable action)
175+
{
176+
try
177+
{
178+
await InvokeAsync(ScrollToSelectedEvent);
179+
}
180+
catch (Exception e)
181+
{
182+
TraceLogger.Trace($"Failed to scroll to selected event on set active table: {e}", LogLevel.Error);
183+
}
184+
}
185+
186+
private async void OnUpdateCombinedEvents(EventTableAction.UpdateCombinedEvents action)
187+
{
188+
try
189+
{
190+
await InvokeAsync(ScrollToSelectedEvent);
191+
}
192+
catch (Exception e)
193+
{
194+
TraceLogger.Trace($"Failed to scroll to selected event on update combined events: {e}", LogLevel.Error);
195+
}
196+
}
197+
198+
private async void OnUpdateDisplayedEvents(EventTableAction.UpdateDisplayedEvents action)
199+
{
200+
try
201+
{
202+
await InvokeAsync(ScrollToSelectedEvent);
203+
}
204+
catch (Exception e)
205+
{
206+
TraceLogger.Trace($"Failed to scroll to selected event on update displayed events: {e}", LogLevel.Error);
207+
}
208+
}
209+
159210
private async Task RegisterTableEventHandlers() => await JSRuntime.InvokeVoidAsync("registerTableEvents");
160211

161212
private async Task ScrollToSelectedEvent()

src/EventLogExpert/MainPage.xaml.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,24 @@ private void PopulateOtherLogsMenu()
439439

440440
private async Task ProcessCommandLine()
441441
{
442-
var args = Environment.GetCommandLineArgs();
443-
444-
foreach (var arg in args)
442+
try
445443
{
446-
switch (arg)
444+
var args = Environment.GetCommandLineArgs();
445+
446+
foreach (var arg in args)
447447
{
448-
case not null when arg.EndsWith(".evtx"):
449-
await OpenLog(arg, PathType.FilePath);
450-
break;
448+
switch (arg)
449+
{
450+
case not null when arg.EndsWith(".evtx"):
451+
await OpenLog(arg, PathType.FilePath);
452+
break;
453+
}
451454
}
452455
}
456+
catch (Exception e)
457+
{
458+
_traceLogger.Trace($"Failed to process command line arguments: {e}", LogLevel.Error);
459+
}
453460
}
454461

455462
private async void ReleaseNotes_Clicked(object sender, EventArgs e) => await _updateService.GetReleaseNotes();

src/EventLogExpert/Shared/Components/DebugLogModal.razor.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using EventLogExpert.UI.Interfaces;
55
using Microsoft.AspNetCore.Components;
6+
using Microsoft.Extensions.Logging;
67

78
namespace EventLogExpert.Shared.Components;
89

@@ -21,7 +22,7 @@ protected internal override async Task Open()
2122

2223
protected override void OnInitialized()
2324
{
24-
TraceLogger.DebugLogLoaded += () => InvokeAsync(Open);
25+
TraceLogger.DebugLogLoaded += OnDebugLogLoaded;
2526

2627
base.OnInitialized();
2728
}
@@ -35,6 +36,18 @@ private async Task Clear()
3536
StateHasChanged();
3637
}
3738

39+
private async void OnDebugLogLoaded()
40+
{
41+
try
42+
{
43+
await InvokeAsync(Open);
44+
}
45+
catch (Exception e)
46+
{
47+
TraceLogger.Trace($"Failed to open debug log modal: {e}", LogLevel.Error);
48+
}
49+
}
50+
3851
private async Task Refresh()
3952
{
4053
_data.Clear();

src/EventLogExpert/Shared/Components/Filters/FilterCacheModal.razor.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// // Licensed under the MIT License.
33

44
using EventLogExpert.UI;
5+
using EventLogExpert.UI.Interfaces;
56
using EventLogExpert.UI.Models;
67
using EventLogExpert.UI.Services;
78
using EventLogExpert.UI.Store.FilterCache;
89
using EventLogExpert.UI.Store.FilterPane;
910
using Fluxor;
1011
using Microsoft.AspNetCore.Components;
12+
using Microsoft.Extensions.Logging;
1113
using System.Text.Json;
1214
using Windows.Storage.Pickers;
1315
using WinRT.Interop;
@@ -23,17 +25,19 @@ public sealed partial class FilterCacheModal
2325

2426
[Inject] private IState<FilterCacheState> FilterCacheState { get; init; } = null!;
2527

28+
[Inject] private IFileLogger TraceLogger { get; init; } = null!;
29+
2630
protected override void OnInitialized()
2731
{
28-
SubscribeToAction<FilterCacheAction.OpenMenu>(action => InvokeAsync(Open));
32+
SubscribeToAction<FilterCacheAction.OpenMenu>(OnOpenMenu);
2933

3034
base.OnInitialized();
3135
}
3236

3337
private void AddFavorite(string filter) =>
3438
Dispatcher.Dispatch(new FilterCacheAction.AddFavoriteFilter(filter));
3539

36-
private void AddFilter(string filter)
40+
private async void AddFilter(string filter)
3741
{
3842
Dispatcher.Dispatch(
3943
new FilterPaneAction.AddFilter(
@@ -44,7 +48,14 @@ private void AddFilter(string filter)
4448
IsEnabled = true
4549
}));
4650

47-
InvokeAsync(Close);
51+
try
52+
{
53+
await InvokeAsync(Close);
54+
}
55+
catch (Exception e)
56+
{
57+
TraceLogger.Trace($"Failed to close filter cache modal: {e}", LogLevel.Error);
58+
}
4859
}
4960

5061
private async Task ExportFavorites()
@@ -119,6 +130,18 @@ await AlertDialogService.ShowAlert("Import Failed",
119130
}
120131
}
121132

133+
private async void OnOpenMenu(FilterCacheAction.OpenMenu action)
134+
{
135+
try
136+
{
137+
await InvokeAsync(Open);
138+
}
139+
catch (Exception e)
140+
{
141+
TraceLogger.Trace($"Failed to open filter cache modal: {e}", LogLevel.Error);
142+
}
143+
}
144+
122145
private void RemoveFavorite(string filter) =>
123146
Dispatcher.Dispatch(new FilterCacheAction.RemoveFavoriteFilter(filter));
124147
}

src/EventLogExpert/Shared/Components/Filters/FilterGroupModal.razor.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// // Copyright (c) Microsoft Corporation.
22
// // Licensed under the MIT License.
33

4+
using EventLogExpert.UI.Interfaces;
45
using EventLogExpert.UI.Models;
56
using EventLogExpert.UI.Services;
67
using EventLogExpert.UI.Store.FilterGroup;
78
using Fluxor;
89
using Microsoft.AspNetCore.Components;
10+
using Microsoft.Extensions.Logging;
911
using System.Text.Json;
1012
using Windows.Storage.Pickers;
1113
using WinRT.Interop;
@@ -21,9 +23,11 @@ public sealed partial class FilterGroupModal
2123

2224
[Inject] private IState<FilterGroupState> FilterGroupState { get; init; } = null!;
2325

26+
[Inject] private IFileLogger TraceLogger { get; init; } = null!;
27+
2428
protected override void OnInitialized()
2529
{
26-
SubscribeToAction<FilterGroupAction.OpenMenu>(action => InvokeAsync(Open));
30+
SubscribeToAction<FilterGroupAction.OpenMenu>(OnOpenMenu);
2731

2832
base.OnInitialized();
2933
}
@@ -99,4 +103,16 @@ await AlertDialogService.ShowAlert("Import Failed",
99103
"OK");
100104
}
101105
}
106+
107+
private async void OnOpenMenu(FilterGroupAction.OpenMenu action)
108+
{
109+
try
110+
{
111+
await InvokeAsync(Open);
112+
}
113+
catch (Exception e)
114+
{
115+
TraceLogger.Trace($"Failed to open filter group modal: {e}", LogLevel.Error);
116+
}
117+
}
102118
}

src/EventLogExpert/Shared/Components/SettingsModal.razor.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace EventLogExpert.Shared.Components;
1717
public sealed partial class SettingsModal
1818
{
1919
private readonly List<(string name, bool isEnabled, bool hasChanged)> _databases = [];
20-
20+
2121
private CopyType _copyType;
2222
private bool _databaseRemoved = false;
2323
private bool _isPreReleaseEnabled;
@@ -38,6 +38,8 @@ public sealed partial class SettingsModal
3838

3939
[Inject] private ISettingsService Settings { get; init; } = null!;
4040

41+
[Inject] private IFileLogger TraceLogger { get; init; } = null!;
42+
4143
protected internal override async Task Close()
4244
{
4345
if (_databaseRemoved)
@@ -56,7 +58,7 @@ protected internal override async Task Close()
5658

5759
protected override void OnInitialized()
5860
{
59-
Settings.Loaded += () => InvokeAsync(Load);
61+
Settings.Loaded += OnSettingsLoaded;
6062

6163
base.OnInitialized();
6264
}
@@ -151,6 +153,18 @@ private async Task Load()
151153
await Open();
152154
}
153155

156+
private async void OnSettingsLoaded()
157+
{
158+
try
159+
{
160+
await InvokeAsync(Load);
161+
}
162+
catch (Exception e)
163+
{
164+
TraceLogger.Trace($"Failed to load settings modal: {e}", LogLevel.Error);
165+
}
166+
}
167+
154168
private async Task ReloadOpenLogs()
155169
{
156170
if (EventLogState.Value.ActiveLogs.IsEmpty) { return; }

0 commit comments

Comments
 (0)