Skip to content

Commit 7f63feb

Browse files
authored
Code Quality: Ignore exceptions in AppLifecycleHelper.HandleAppUnhandledException (#15958)
1 parent 2094466 commit 7f63feb

File tree

1 file changed

+71
-66
lines changed

1 file changed

+71
-66
lines changed

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -250,94 +250,99 @@ public static void SaveSessionTabs()
250250
/// </summary>
251251
public static void HandleAppUnhandledException(Exception? ex, bool showToastNotification)
252252
{
253-
var generalSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
254-
255-
StringBuilder formattedException = new()
256-
{
257-
Capacity = 200
258-
};
259-
260-
formattedException.AppendLine("--------- UNHANDLED EXCEPTION ---------");
261-
262-
if (ex is not null)
253+
SafetyExtensions.IgnoreExceptions(() =>
263254
{
264-
ex.Data[Mechanism.HandledKey] = false;
265-
ex.Data[Mechanism.MechanismKey] = "Application.UnhandledException";
255+
var generalSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
266256

267-
SentrySdk.CaptureException(ex, scope =>
257+
StringBuilder formattedException = new()
268258
{
269-
scope.User.Id = generalSettingsService?.UserId;
270-
scope.Level = SentryLevel.Fatal;
271-
});
259+
Capacity = 200
260+
};
272261

273-
formattedException.AppendLine($">>>> HRESULT: {ex.HResult}");
262+
formattedException.AppendLine("--------- UNHANDLED EXCEPTION ---------");
274263

275-
if (ex.Message is not null)
276-
{
277-
formattedException.AppendLine("--- MESSAGE ---");
278-
formattedException.AppendLine(ex.Message);
279-
}
280-
if (ex.StackTrace is not null)
264+
if (ex is not null)
281265
{
282-
formattedException.AppendLine("--- STACKTRACE ---");
283-
formattedException.AppendLine(ex.StackTrace);
266+
ex.Data[Mechanism.HandledKey] = false;
267+
ex.Data[Mechanism.MechanismKey] = "Application.UnhandledException";
268+
269+
SentrySdk.CaptureException(ex, scope =>
270+
{
271+
scope.User.Id = generalSettingsService?.UserId;
272+
scope.Level = SentryLevel.Fatal;
273+
});
274+
275+
formattedException.AppendLine($">>>> HRESULT: {ex.HResult}");
276+
277+
if (ex.Message is not null)
278+
{
279+
formattedException.AppendLine("--- MESSAGE ---");
280+
formattedException.AppendLine(ex.Message);
281+
}
282+
if (ex.StackTrace is not null)
283+
{
284+
formattedException.AppendLine("--- STACKTRACE ---");
285+
formattedException.AppendLine(ex.StackTrace);
286+
}
287+
if (ex.Source is not null)
288+
{
289+
formattedException.AppendLine("--- SOURCE ---");
290+
formattedException.AppendLine(ex.Source);
291+
}
292+
if (ex.InnerException is not null)
293+
{
294+
formattedException.AppendLine("--- INNER ---");
295+
formattedException.AppendLine(ex.InnerException.ToString());
296+
}
284297
}
285-
if (ex.Source is not null)
286-
{
287-
formattedException.AppendLine("--- SOURCE ---");
288-
formattedException.AppendLine(ex.Source);
289-
}
290-
if (ex.InnerException is not null)
298+
else
291299
{
292-
formattedException.AppendLine("--- INNER ---");
293-
formattedException.AppendLine(ex.InnerException.ToString());
300+
formattedException.AppendLine("Exception data is not available.");
294301
}
295-
}
296-
else
297-
{
298-
formattedException.AppendLine("Exception data is not available.");
299-
}
300302

301-
formattedException.AppendLine("---------------------------------------");
303+
formattedException.AppendLine("---------------------------------------");
302304

303-
Debug.WriteLine(formattedException.ToString());
305+
Debug.WriteLine(formattedException.ToString());
304306

305-
// Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
306-
Debugger.Break();
307+
// Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
308+
Debugger.Break();
309+
310+
App.Logger?.LogError(ex, ex?.Message ?? "An unhandled error occurred.");
311+
});
307312

308313
// Save the current tab list in case it was overwriten by another instance
309-
SaveSessionTabs();
310-
App.Logger?.LogError(ex, ex?.Message ?? "An unhandled error occurred.");
314+
SafetyExtensions.IgnoreExceptions(SaveSessionTabs);
311315

312316
if (!showToastNotification)
313317
return;
314318

315-
SafetyExtensions.IgnoreExceptions(() =>
316-
{
317-
AppToastNotificationHelper.ShowUnhandledExceptionToast();
318-
});
319-
320-
// Restart the app
321-
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
322-
var lastSessionTabList = userSettingsService.GeneralSettingsService.LastSessionTabList;
319+
SafetyExtensions.IgnoreExceptions(AppToastNotificationHelper.ShowUnhandledExceptionToast);
323320

324-
if (userSettingsService.GeneralSettingsService.LastCrashedTabList?.SequenceEqual(lastSessionTabList) ?? false)
325-
{
326-
// Avoid infinite restart loop
327-
userSettingsService.GeneralSettingsService.LastSessionTabList = null;
328-
}
329-
else
321+
SafetyExtensions.IgnoreExceptions(() =>
330322
{
331-
userSettingsService.AppSettingsService.RestoreTabsOnStartup = true;
332-
userSettingsService.GeneralSettingsService.LastCrashedTabList = lastSessionTabList;
323+
// Restart the app
324+
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
325+
var lastSessionTabList = userSettingsService.GeneralSettingsService.LastSessionTabList;
333326

334-
// Try to re-launch and start over
335-
MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
327+
if (userSettingsService.GeneralSettingsService.LastCrashedTabList?.SequenceEqual(lastSessionTabList) ?? false)
336328
{
337-
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
338-
})
339-
.Wait(100);
340-
}
329+
// Avoid infinite restart loop
330+
userSettingsService.GeneralSettingsService.LastSessionTabList = null;
331+
}
332+
else
333+
{
334+
userSettingsService.AppSettingsService.RestoreTabsOnStartup = true;
335+
userSettingsService.GeneralSettingsService.LastCrashedTabList = lastSessionTabList;
336+
337+
// Try to re-launch and start over
338+
MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
339+
{
340+
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
341+
})
342+
.Wait(100);
343+
}
344+
});
345+
341346
Process.GetCurrentProcess().Kill();
342347
}
343348

0 commit comments

Comments
 (0)