Skip to content

Commit 4fa789d

Browse files
committed
added more logging and exception catching.
1 parent e7d4ca0 commit 4fa789d

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.InteropServices;
@@ -38,14 +39,22 @@ public void Localize()
3839
return;
3940
}
4041

41-
foreach (var kvp in _items)
42+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
4243
{
43-
var item = kvp;
44-
UiDispatcher.Invoke(() =>
44+
try
4545
{
46-
item.Value.Caption = item.Key.Caption.Invoke();
47-
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
48-
});
46+
var item = kvp;
47+
UiDispatcher.Invoke(() =>
48+
{
49+
item.Value.Caption = item.Key.Caption.Invoke();
50+
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
51+
});
52+
53+
}
54+
catch (Exception e)
55+
{
56+
Logger.Error(e, $"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
57+
}
4958
}
5059
}
5160

@@ -92,17 +101,23 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
92101

93102
public void EvaluateCanExecute(RubberduckParserState state)
94103
{
95-
foreach (var kvp in _items)
104+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
96105
{
97106
var commandItem = kvp.Key;
98-
if (commandItem != null && kvp.Value != null)
107+
var canExecute = false;
108+
try
99109
{
100-
var canExecute = commandItem.EvaluateCanExecute(state);
101-
kvp.Value.IsEnabled = canExecute;
102-
if (commandItem.HiddenWhenDisabled)
103-
{
104-
kvp.Value.IsVisible = canExecute;
105-
}
110+
canExecute = commandItem.EvaluateCanExecute(state);
111+
112+
}
113+
catch (Exception e)
114+
{
115+
Logger.Error(e, $"{commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}.EvaluateCanExecute(RubberduckParserState) threw an exception.");
116+
}
117+
kvp.Value.IsEnabled = canExecute;
118+
if (commandItem?.HiddenWhenDisabled ?? false)
119+
{
120+
kvp.Value.IsVisible = canExecute;
106121
}
107122
}
108123
}

0 commit comments

Comments
 (0)