Skip to content

Commit 096c085

Browse files
committed
Moved wrongly positioned try catch into the dispatched delegate in AppCommandBarBase and added further try catch blocks around COM interaction.
1 parent f81cd5b commit 096c085

File tree

1 file changed

+74
-25
lines changed

1 file changed

+74
-25
lines changed

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

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class AppCommandBarBase : IAppCommandBar
1515
private readonly string _name;
1616
private readonly CommandBarPosition _position;
1717
private readonly IDictionary<ICommandMenuItem, ICommandBarControl> _items;
18-
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
18+
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();
1919

2020
protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerable<ICommandMenuItem> items)
2121
{
@@ -26,10 +26,18 @@ protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerabl
2626

2727
protected ICommandMenuItem FindChildByTag(string tag)
2828
{
29-
var child = _items.FirstOrDefault(kvp => kvp.Value != null && kvp.Value.Tag == tag);
30-
return Equals(child, default(KeyValuePair<ICommandMenuItem, ICommandBarControl>))
31-
? null
32-
: child.Key;
29+
try
30+
{
31+
var child = _items.FirstOrDefault(kvp => kvp.Value != null && kvp.Value.Tag == tag);
32+
return Equals(child, default(KeyValuePair<ICommandMenuItem, ICommandBarControl>))
33+
? null
34+
: child.Key;
35+
}
36+
catch (COMException exception)
37+
{
38+
Logger.Error(exception,$"COMException while finding child with tag '{tag}'.");
39+
}
40+
return null;
3341
}
3442

3543
public void Localize()
@@ -39,34 +47,52 @@ public void Localize()
3947
return;
4048
}
4149

42-
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
50+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null && !kv.Value.IsWrappingNullReference))
4351
{
4452
try
4553
{
4654
var item = kvp;
47-
UiDispatcher.Invoke(() =>
48-
{
49-
item.Value.Caption = item.Key.Caption.Invoke();
50-
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
51-
});
55+
UiDispatcher.Invoke(() => LocalizeInternal(item, kvp));
5256

5357
}
5458
catch (Exception e)
5559
{
56-
Logger.Error(e, $"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
60+
Logger.Error(e, $"Failed to dispatch assignment of {kvp.Value.GetType().Name}.Caption and .TooltipText for {kvp.Key.GetType().Name} to the UI thread.");
5761
}
5862
}
5963
}
6064

65+
private static void LocalizeInternal(KeyValuePair<ICommandMenuItem, ICommandBarControl> item, KeyValuePair<ICommandMenuItem, ICommandBarControl> kvp)
66+
{
67+
try
68+
{
69+
item.Value.Caption = item.Key.Caption.Invoke();
70+
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
71+
}
72+
catch (Exception e)
73+
{
74+
Logger.Error(e,
75+
$"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
76+
}
77+
}
78+
6179
public virtual void Initialize()
6280
{
63-
if (Parent == null)
81+
if (Parent == null || Parent.IsWrappingNullReference)
6482
{
6583
return;
6684
}
6785

68-
Item = Parent.Add(_name, _position);
69-
Item.IsVisible = true;
86+
try
87+
{
88+
Item = Parent.Add(_name, _position);
89+
Item.IsVisible = true;
90+
}
91+
catch (COMException exception)
92+
{
93+
Logger.Error(exception, $"Failed to add the command bar {_name}.");
94+
return;
95+
}
7096
foreach (var item in _items.Keys.OrderBy(item => item.DisplayOrder))
7197
{
7298
try
@@ -109,7 +135,7 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
109135

110136
public void EvaluateCanExecute(RubberduckParserState state)
111137
{
112-
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
138+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null && !kv.Value.IsWrappingNullReference))
113139
{
114140
var commandItem = kvp.Key;
115141
var canExecute = false;
@@ -122,10 +148,17 @@ public void EvaluateCanExecute(RubberduckParserState state)
122148
{
123149
Logger.Error(e, $"{commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}.EvaluateCanExecute(RubberduckParserState) threw an exception.");
124150
}
125-
kvp.Value.IsEnabled = canExecute;
126-
if (commandItem?.HiddenWhenDisabled ?? false)
151+
try
152+
{
153+
kvp.Value.IsEnabled = canExecute;
154+
if (commandItem?.HiddenWhenDisabled ?? false)
155+
{
156+
kvp.Value.IsVisible = canExecute;
157+
}
158+
}
159+
catch (COMException exception)
127160
{
128-
kvp.Value.IsVisible = canExecute;
161+
Logger.Error(exception,$"COMException while trying to set IsEnabled and IsVisible on {commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}");
129162
}
130163
}
131164
}
@@ -135,11 +168,18 @@ public void EvaluateCanExecute(RubberduckParserState state)
135168

136169
public void RemoveCommandBar()
137170
{
138-
Logger.Debug("Removing commandbar.");
139-
RemoveChildren();
140-
Item?.Delete();
141-
Item = null;
142-
Parent = null;
171+
try
172+
{
173+
Logger.Debug("Removing commandbar.");
174+
RemoveChildren();
175+
Item?.Delete();
176+
Item = null;
177+
Parent = null;
178+
}
179+
catch (COMException exception)
180+
{
181+
Logger.Error(exception, "COM exception while trying to delee the commandbar");
182+
}
143183
}
144184

145185
private void RemoveChildren()
@@ -169,7 +209,16 @@ private void RemoveChildren()
169209

170210
private void child_Click(object sender, CommandBarButtonClickEventArgs e)
171211
{
172-
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
212+
ICommandMenuItem item;
213+
try
214+
{
215+
item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
216+
}
217+
catch (COMException exception)
218+
{
219+
Logger.Error(exception, "COM exception finding command for a control.");
220+
item = null;
221+
}
173222
if (item == null)
174223
{
175224
return;

0 commit comments

Comments
 (0)