Skip to content

Commit 67db28a

Browse files
authored
Code Quality: Revert "Ignore exceptions in AppLifecycleHelper.HandleAppUnhandledException" (#16078)
1 parent 678fe9c commit 67db28a

File tree

2 files changed

+70
-91
lines changed

2 files changed

+70
-91
lines changed

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

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

259-
StringBuilder formattedException = new()
269+
SentrySdk.CaptureException(ex, scope =>
260270
{
261-
Capacity = 200
262-
};
271+
scope.User.Id = generalSettingsService?.UserId;
272+
scope.Level = SentryLevel.Fatal;
273+
});
263274

264-
formattedException.AppendLine("--------- UNHANDLED EXCEPTION ---------");
275+
formattedException.AppendLine($">>>> HRESULT: {ex.HResult}");
265276

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

305-
formattedException.AppendLine("---------------------------------------");
306-
307-
Debug.WriteLine(formattedException.ToString());
303+
formattedException.AppendLine("---------------------------------------");
308304

309-
// Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
310-
Debugger.Break();
305+
Debug.WriteLine(formattedException.ToString());
311306

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

315310
// Save the current tab list in case it was overwriten by another instance
316-
SafetyExtensions.IgnoreExceptions(SaveSessionTabs);
311+
SaveSessionTabs();
312+
App.Logger?.LogError(ex, ex?.Message ?? "An unhandled error occurred.");
317313

318314
if (!showToastNotification)
319315
return;
320316

321-
SafetyExtensions.IgnoreExceptions(AppToastNotificationHelper.ShowUnhandledExceptionToast);
322-
323317
SafetyExtensions.IgnoreExceptions(() =>
324318
{
325-
// Restart the app
326-
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
327-
var lastSessionTabList = userSettingsService.GeneralSettingsService.LastSessionTabList;
328-
329-
if (userSettingsService.GeneralSettingsService.LastCrashedTabList?.SequenceEqual(lastSessionTabList) ?? false)
330-
{
331-
// Avoid infinite restart loop
332-
userSettingsService.GeneralSettingsService.LastSessionTabList = null;
333-
}
334-
else
335-
{
336-
userSettingsService.AppSettingsService.RestoreTabsOnStartup = true;
337-
userSettingsService.GeneralSettingsService.LastCrashedTabList = lastSessionTabList;
338-
339-
// Try to re-launch and start over
340-
MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
341-
{
342-
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
343-
})
344-
.Wait(100);
345-
}
319+
AppToastNotificationHelper.ShowUnhandledExceptionToast();
346320
});
347321

322+
// Restart the app
323+
var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
324+
var lastSessionTabList = userSettingsService.GeneralSettingsService.LastSessionTabList;
325+
326+
if (userSettingsService.GeneralSettingsService.LastCrashedTabList?.SequenceEqual(lastSessionTabList) ?? false)
327+
{
328+
// Avoid infinite restart loop
329+
userSettingsService.GeneralSettingsService.LastSessionTabList = null;
330+
}
331+
else
332+
{
333+
userSettingsService.AppSettingsService.RestoreTabsOnStartup = true;
334+
userSettingsService.GeneralSettingsService.LastCrashedTabList = lastSessionTabList;
335+
336+
// Try to re-launch and start over
337+
MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
338+
{
339+
await Launcher.LaunchUriAsync(new Uri("files-uwp:"));
340+
})
341+
.Wait(100);
342+
}
348343
Process.GetCurrentProcess().Kill();
349344
}
350345

src/Files.Shared/Extensions/SafetyExtensions.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ public static bool IgnoreExceptions(Action action, ILogger? logger = null, Type?
2020
{
2121
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
2222
{
23-
try
24-
{
25-
logger?.LogInformation(ex, ex.Message);
26-
}
27-
catch { }
23+
logger?.LogInformation(ex, ex.Message);
2824

2925
return false;
3026
}
@@ -45,11 +41,7 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
4541
{
4642
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
4743
{
48-
try
49-
{
50-
logger?.LogInformation(ex, ex.Message);
51-
}
52-
catch { }
44+
logger?.LogInformation(ex, ex.Message);
5345

5446
return false;
5547
}
@@ -68,11 +60,7 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
6860
{
6961
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
7062
{
71-
try
72-
{
73-
logger?.LogInformation(ex, ex.Message);
74-
}
75-
catch { }
63+
logger?.LogInformation(ex, ex.Message);
7664

7765
return default;
7866
}
@@ -91,11 +79,7 @@ public static async Task<bool> IgnoreExceptions(Func<Task> action, ILogger? logg
9179
{
9280
if (exceptionToIgnore is null || exceptionToIgnore.IsAssignableFrom(ex.GetType()))
9381
{
94-
try
95-
{
96-
logger?.LogInformation(ex, ex.Message);
97-
}
98-
catch { }
82+
logger?.LogInformation(ex, ex.Message);
9983

10084
return default;
10185
}

0 commit comments

Comments
 (0)