@@ -137,19 +137,26 @@ private static async Task UnloadProjectAsync(Guid identifier, State state) {
137137
138138 if ( IsLoaded ( state . Solution , identifier ) ) {
139139 string name ;
140- int result ;
141140
142141
143142 name = state . Solution . GetName ( identifier ) ;
144143 state . SetProgressText ( $ "Unloading { name } ...") ;
145144
146- result = state . Solution . UnloadProject (
147- identifier ,
148- ( uint ) _VSProjectUnloadStatus . UNLOADSTATUS_UnloadedByUser
149- ) ;
145+ try {
146+ int result ;
147+
148+
149+ result = state . Solution . UnloadProject (
150+ identifier ,
151+ ( uint ) _VSProjectUnloadStatus . UNLOADSTATUS_UnloadedByUser
152+ ) ;
150153
151- if ( ErrorHandler . Failed ( result ) ) {
152- await LogFailureAsync ( $ "Failed to unload project '{ name } '.", result ) ;
154+ if ( ErrorHandler . Failed ( result ) ) {
155+ await LogFailureAsync ( $ "Failed to unload project '{ name } '.", result ) ;
156+ }
157+
158+ } catch ( Exception ex ) when ( ! ErrorHandler . IsCriticalException ( ex ) ) {
159+ await LogFailureAsync ( $ "Failed to unload project '{ name } '.", ex ) ;
153160 }
154161
155162 state . OnProjectUnloaded ( identifier ) ;
@@ -166,23 +173,33 @@ private static async Task LoadProjectAsync(Guid identifier, bool loadProjectDepe
166173 // visited it before, then we don't need to do anything this time.
167174 if ( state . ProjectsVisitedWhileLoading . Add ( identifier ) ) {
168175 IEnumerable < Guid > ? dependencies ;
169- bool loaded ;
176+ bool wasLoaded ;
170177
171178
172179 dependencies = null ;
173- loaded = false ;
180+ wasLoaded = false ;
174181
175182 if ( ! IsLoaded ( state . Solution , identifier ) ) {
176183 string name ;
177- int result ;
178184
179185
180186 name = state . Solution . GetName ( identifier ) ;
181187 state . SetProgressText ( $ "Loading { name } ...") ;
182- result = state . Solution . ReloadProject ( identifier ) ;
183188
184- if ( ErrorHandler . Failed ( result ) ) {
185- await LogFailureAsync ( $ "Failed to load project '{ name } '.", result ) ;
189+ try {
190+ int result ;
191+
192+
193+ result = state . Solution . ReloadProject ( identifier ) ;
194+
195+ if ( ErrorHandler . Failed ( result ) ) {
196+ await LogFailureAsync ( $ "Failed to load project '{ name } '.", result ) ;
197+ return ;
198+ }
199+
200+ } catch ( Exception ex ) when ( ! ErrorHandler . IsCriticalException ( ex ) ) {
201+ await LogFailureAsync ( $ "Failed to load project '{ name } '.", ex ) ;
202+ return ;
186203 }
187204
188205 // Don't record that we've loaded the project _yet_. If we do that
@@ -193,7 +210,7 @@ private static async Task LoadProjectAsync(Guid identifier, bool loadProjectDepe
193210 // the progress will increase to 100% and then immediately decrease.
194211 // We'll wait until we've recorded that the dependencies need to be
195212 // loaded before we record that this project was loaded.
196- loaded = true ;
213+ wasLoaded = true ;
197214
198215 // Whenever a project is loaded, we should recalculate project dependencies
199216 // to ensure that the new project's dependencies are correct.
@@ -219,7 +236,7 @@ private static async Task LoadProjectAsync(Guid identifier, bool loadProjectDepe
219236 // if we actually loaded the given project, then we can record that
220237 // it has been loaded. This prevents the progress from increasing
221238 // and then decreasing (instead, it will decrease and then increase).
222- if ( loaded ) {
239+ if ( wasLoaded ) {
223240 state . OnProjectLoaded ( identifier ) ;
224241 }
225242
@@ -357,13 +374,21 @@ private static async Task<IEnumerable<Guid>> GetProjectsToCollapseAsync(ISolutio
357374 }
358375
359376
360- private static async Task LogFailureAsync ( string message , int result ) {
377+ private static Task LogFailureAsync ( string message , int result ) {
378+ return LogFailureAsync ( message , Marshal . GetExceptionForHR ( result ) ) ;
379+ }
380+
381+
382+ private static async Task LogFailureAsync ( string message , Exception ? ex ) {
361383 ILogger logger ;
384+ string exceptionMessage ;
362385
363386
364387 logger = await VS . GetRequiredServiceAsync < ILogger , ILogger > ( ) ;
365388
366- await logger . WriteLineAsync ( $ "{ message } { Marshal . GetExceptionForHR ( result ) . Message } ") ;
389+ exceptionMessage = ex ? . Message ?? "[Unknown Error]" ;
390+
391+ await logger . WriteLineAsync ( $ "{ message } { exceptionMessage } ") ;
367392 }
368393
369394}
0 commit comments