Skip to content

Commit a51d356

Browse files
committed
Actually catch the right exception at the correct place
COMExceptions were already handled in FindChildByTag in AppCommandBar. The problem is that the faulty registration causes an InvalidCastException.
1 parent 94197a9 commit a51d356

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

Rubberduck.Core/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ protected ICommandMenuItem FindChildByTag(string tag)
3737
}
3838
catch (COMException exception)
3939
{
40-
Logger.Error(exception,$"COMException while finding child with tag '{tag}'.");
40+
Logger.Error(exception, $"COMException while finding child with tag '{tag}' in the command bar.");
41+
}
42+
catch (InvalidCastException exception)
43+
{
44+
//This exception will be encountered whenever the registration of the command bar control not correct in the system.
45+
//See issue #5349 at https://github.com/rubberduck-vba/Rubberduck/issues/5349
46+
Logger.Error(exception, $"Invalid cast exception while finding child with tag '{tag}' in the command bar.");
4147
}
4248
return null;
4349
}

Rubberduck.Core/UI/Command/MenuItems/CommandBars/RubberduckCommandBar.cs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -99,49 +99,39 @@ public void SetStatusLabelCaption(ParserState state, int? errorCount = null)
9999

100100
private void SetStatusLabelCaption(string caption, int? errorCount = null)
101101
{
102-
//This try-catch block guarantees that problems with the COM registration of the command bar classes
103-
//only affect the status label text instead of aborting the status change or even crashing the host.
104-
//See issue #5349 at https://github.com/rubberduck-vba/Rubberduck/issues/5349
105-
try
102+
var reparseCommandButton =
103+
FindChildByTag(typeof(ReparseCommandMenuItem).FullName) as ReparseCommandMenuItem;
104+
if (reparseCommandButton == null)
106105
{
107-
var reparseCommandButton =
108-
FindChildByTag(typeof(ReparseCommandMenuItem).FullName) as ReparseCommandMenuItem;
109-
if (reparseCommandButton == null)
110-
{
111-
return;
112-
}
106+
return;
107+
}
113108

114-
var showErrorsCommandButton =
115-
FindChildByTag(typeof(ShowParserErrorsCommandMenuItem).FullName) as ShowParserErrorsCommandMenuItem;
116-
if (showErrorsCommandButton == null)
117-
{
118-
return;
119-
}
109+
var showErrorsCommandButton =
110+
FindChildByTag(typeof(ShowParserErrorsCommandMenuItem).FullName) as ShowParserErrorsCommandMenuItem;
111+
if (showErrorsCommandButton == null)
112+
{
113+
return;
114+
}
120115

121-
_uiDispatcher.Invoke(() =>
116+
_uiDispatcher.Invoke(() =>
117+
{
118+
try
122119
{
123-
try
120+
reparseCommandButton.SetCaption(caption);
121+
reparseCommandButton.SetToolTip(string.Format(RubberduckUI.ReparseToolTipText, caption));
122+
if (errorCount.HasValue && errorCount.Value > 0)
124123
{
125-
reparseCommandButton.SetCaption(caption);
126-
reparseCommandButton.SetToolTip(string.Format(RubberduckUI.ReparseToolTipText, caption));
127-
if (errorCount.HasValue && errorCount.Value > 0)
128-
{
129-
showErrorsCommandButton.SetToolTip(
130-
string.Format(RubberduckUI.ParserErrorToolTipText, errorCount.Value));
131-
}
124+
showErrorsCommandButton.SetToolTip(
125+
string.Format(RubberduckUI.ParserErrorToolTipText, errorCount.Value));
132126
}
133-
catch (Exception exception)
134-
{
135-
Logger.Error(exception,
136-
"Exception thrown trying to set the status label caption on the UI thread.");
137-
}
138-
});
139-
Localize();
140-
}
141-
catch (COMException exception)
142-
{
143-
Logger.Error(exception, "COMException thrown trying to set the status label caption.");
144-
}
127+
}
128+
catch (Exception exception)
129+
{
130+
Logger.Error(exception,
131+
"Exception thrown trying to set the status label caption on the UI thread.");
132+
}
133+
});
134+
Localize();
145135
}
146136

147137
private void SetContextSelectionCaption(string caption, int contextReferenceCount, string description)

0 commit comments

Comments
 (0)