Skip to content

Commit 7ebed46

Browse files
committed
Even more catching and logging
1 parent acd485f commit 7ebed46

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

Rubberduck.VBEEditor/SafeComWrappers/Abstract/HostApplicationBase.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ private static TApplication ApplicationFromComReflection(string applicationName)
2626
{
2727
application = (TApplication) Marshal.GetActiveObject($"{applicationName}.Application");
2828
}
29-
catch (COMException)
29+
catch (COMException exception)
3030
{
31-
application = null; // unit tests don't need it anyway.
31+
_logger.Error(exception, $"Unexpected COM exception while acquiring the host application object for application {applicationName} via COM reflection.");
32+
application = null; // We currently really only use the name anyway.
3233
}
3334
catch (InvalidCastException exception)
3435
{
3536
//TODO: Find out why this ever happens.
36-
_logger.Error(exception, $"Unable to cast the host Application {applicationName} to its PIA type.");
37+
_logger.Error(exception, $"Unable to cast the host application object for application {applicationName} acquired via COM reflection to its PIA type.");
38+
application = null; //We currently really only use the name anyway.
39+
}
40+
catch (Exception exception)
41+
{
42+
//note: We catch all exceptions because we currently really do not need application object and there can be exceptions for unexpected system setups.
43+
_logger.Error(exception, $"Unexpected exception while acquiring the host application object for application {applicationName} from a document module.");
3744
application = null; //We currently really only use the name anyway.
3845
}
3946
return application;
@@ -44,24 +51,33 @@ private static TApplication ApplicationFromVbe(IVBE vbe, string applicationName)
4451
TApplication application;
4552
try
4653
{
47-
var appProperty = ApplicationPropertyFromDocumentModule(vbe);
48-
if (appProperty != null)
54+
using (var appProperty = ApplicationPropertyFromDocumentModule(vbe))
4955
{
50-
application = (TApplication)appProperty.Object;
51-
}
52-
else
53-
{
54-
application = (TApplication)Marshal.GetActiveObject($"{applicationName}.Application");
56+
if (appProperty != null)
57+
{
58+
application = (TApplication) appProperty.Object;
59+
}
60+
else
61+
{
62+
application = ApplicationFromComReflection(applicationName);
63+
}
5564
}
5665

5766
}
58-
catch (COMException)
67+
catch (COMException exception)
5968
{
60-
application = null; // unit tests don't need it anyway.
69+
_logger.Error(exception, $"Unexpected COM exception while acquiring the host application object for application {applicationName} from a document module.");
70+
application = null; // We currently really only use the name anyway.
6171
}
6272
catch (InvalidCastException exception)
6373
{
64-
_logger.Error(exception, $"Unable to cast the host Application {applicationName} to its PIA type.");
74+
_logger.Error(exception, $"Unable to cast the host application object for application {applicationName} acquiered from a document module to its PIA type.");
75+
application = null; //We currently really only use the name anyway.
76+
}
77+
catch (Exception exception)
78+
{
79+
//note: We catch all exceptions because we currently really do not need application object and there can be exceptions for unexpected system setups.
80+
_logger.Error(exception, $"Unexpected exception while acquiring the host application object for application {applicationName} from a document module.");
6581
application = null; //We currently really only use the name anyway.
6682
}
6783
return application;

0 commit comments

Comments
 (0)