Skip to content

Commit 152c719

Browse files
authored
fix(command): add error handling for Execute method (#61)
1 parent 547a27b commit 152c719

1 file changed

Lines changed: 79 additions & 8 deletions

File tree

src/CodingWithCalvin.ProjectRenamifier/Commands/RenamifyProjectCommand.cs

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,91 @@ private void Execute(object sender, EventArgs e)
4646
{
4747
ThreadHelper.ThrowIfNotOnUIThread();
4848

49-
using var activity = VsixTelemetry.StartCommandActivity("ProjectRenamifier.Execute");
50-
51-
if (!(ServiceProvider.GetService(typeof(DTE)) is DTE2 dte))
49+
IDisposable activity = null;
50+
try
5251
{
53-
return;
52+
activity = VsixTelemetry.StartCommandActivity("ProjectRenamifier.Execute");
53+
}
54+
catch (Exception ex)
55+
{
56+
// Telemetry initialization failed - log but continue
57+
System.Diagnostics.Debug.WriteLine($"Telemetry initialization failed: {ex.Message}");
5458
}
5559

56-
var selectedItems = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems;
57-
foreach (UIHierarchyItem selectedItem in selectedItems)
60+
try
5861
{
59-
if (selectedItem.Object is Project project)
62+
if (!(ServiceProvider.GetService(typeof(DTE)) is DTE2 dte))
63+
{
64+
System.Windows.MessageBox.Show(
65+
"Unable to access Visual Studio automation services (DTE).\n\n" +
66+
"Please try again or restart Visual Studio.",
67+
"Project Renamifier",
68+
System.Windows.MessageBoxButton.OK,
69+
System.Windows.MessageBoxImage.Warning);
70+
return;
71+
}
72+
73+
Array selectedItems;
74+
try
75+
{
76+
selectedItems = (Array)dte.ToolWindows.SolutionExplorer.SelectedItems;
77+
}
78+
catch (Exception ex)
6079
{
61-
RenameProject(project, dte);
80+
System.Windows.MessageBox.Show(
81+
$"Unable to access Solution Explorer selection.\n\n{ex.Message}",
82+
"Project Renamifier",
83+
System.Windows.MessageBoxButton.OK,
84+
System.Windows.MessageBoxImage.Warning);
85+
return;
6286
}
87+
88+
if (selectedItems == null || selectedItems.Length == 0)
89+
{
90+
System.Windows.MessageBox.Show(
91+
"No project is selected.\n\nPlease select a project in Solution Explorer and try again.",
92+
"Project Renamifier",
93+
System.Windows.MessageBoxButton.OK,
94+
System.Windows.MessageBoxImage.Information);
95+
return;
96+
}
97+
98+
var projectFound = false;
99+
foreach (UIHierarchyItem selectedItem in selectedItems)
100+
{
101+
if (selectedItem.Object is Project project)
102+
{
103+
projectFound = true;
104+
RenameProject(project, dte);
105+
}
106+
}
107+
108+
if (!projectFound)
109+
{
110+
System.Windows.MessageBox.Show(
111+
"The selected item is not a project.\n\nPlease select a project (not a solution folder or file) and try again.",
112+
"Project Renamifier",
113+
System.Windows.MessageBoxButton.OK,
114+
System.Windows.MessageBoxImage.Information);
115+
}
116+
}
117+
catch (Exception ex)
118+
{
119+
VsixTelemetry.TrackException(ex, new Dictionary<string, object>
120+
{
121+
{ "operation.name", "Execute" }
122+
});
123+
124+
System.Windows.MessageBox.Show(
125+
$"An unexpected error occurred:\n\n{ex.Message}\n\n" +
126+
"Please report this issue at:\nhttps://github.com/CodingWithCalvin/VS-ProjectRenamifier/issues",
127+
"Project Renamifier - Error",
128+
System.Windows.MessageBoxButton.OK,
129+
System.Windows.MessageBoxImage.Error);
130+
}
131+
finally
132+
{
133+
activity?.Dispose();
63134
}
64135
}
65136

0 commit comments

Comments
 (0)