Skip to content

Commit b974473

Browse files
committed
Requested changes
1 parent 3fb4812 commit b974473

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

src/Files.App/App.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public App()
3838
InitializeComponent();
3939

4040
// Configure exception handlers
41-
UnhandledException += AppLifecycleHelper.App_UnhandledException;
42-
AppDomain.CurrentDomain.UnhandledException += AppLifecycleHelper.CurrentDomain_UnhandledException;
43-
TaskScheduler.UnobservedTaskException += AppLifecycleHelper.TaskScheduler_UnobservedTaskException;
41+
UnhandledException += (sender, e) => AppLifecycleHelper.HandleAppUnhandledException(e.Exception, true);
42+
AppDomain.CurrentDomain.UnhandledException += (sender, e) => AppLifecycleHelper.HandleAppUnhandledException(e.ExceptionObject as Exception, false);
43+
TaskScheduler.UnobservedTaskException += (sender, e) => AppLifecycleHelper.HandleAppUnhandledException(e.Exception, false);
4444
}
4545

4646
/// <summary>

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

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -200,83 +200,57 @@ public static void SaveSessionTabs()
200200
.ToList();
201201
}
202202

203-
/// <summary>
204-
/// Invoked when an exception is not handled on the UI thread.
205-
/// </summary>
206-
public static void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
207-
{
208-
AppUnhandledException(e.Exception, true);
209-
}
210-
211-
/// <summary>
212-
/// Invoked when an exception is not handled on the current thread.
213-
/// </summary>
214-
public static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
215-
{
216-
AppUnhandledException(e.ExceptionObject as Exception, false);
217-
}
218-
219-
/// <summary>
220-
/// Invoked when an exception is not handled on a background thread.
221-
/// </summary>
222-
/// <remarks>
223-
/// This exception is sometimes occurred by forgotten <c>Task.Run(() => {...})</c>.
224-
/// </remarks>
225-
public static void TaskScheduler_UnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
226-
{
227-
AppUnhandledException(e.Exception, false);
228-
}
229-
230203
/// <summary>
231204
/// Shows exception on the Debug Output and sends Toast Notification to the Windows Notification Center.
232205
/// </summary>
233-
private static void AppUnhandledException(Exception? ex, bool showToastNotification)
206+
public static void HandleAppUnhandledException(Exception? ex, bool showToastNotification)
234207
{
235208
StringBuilder formattedException = new()
236209
{
237210
Capacity = 200
238211
};
239212

240-
formattedException.Append("--------- UNHANDLED EXCEPTION ---------");
213+
formattedException.AppendLine("--------- UNHANDLED EXCEPTION ---------");
241214

242215
if (ex is not null)
243216
{
244-
formattedException.Append($"\n>>>> HRESULT: {ex.HResult}\n");
217+
formattedException.AppendLine($">>>> HRESULT: {ex.HResult}");
218+
245219
if (ex.Message is not null)
246220
{
247-
formattedException.Append("\n--- MESSAGE ---");
248-
formattedException.Append(ex.Message);
221+
formattedException.AppendLine("--- MESSAGE ---");
222+
formattedException.AppendLine(ex.Message);
249223
}
250224
if (ex.StackTrace is not null)
251225
{
252-
formattedException.Append("\n--- STACKTRACE ---");
253-
formattedException.Append(ex.StackTrace);
226+
formattedException.AppendLine("--- STACKTRACE ---");
227+
formattedException.AppendLine(ex.StackTrace);
254228
}
255229
if (ex.Source is not null)
256230
{
257-
formattedException.Append("\n--- SOURCE ---");
258-
formattedException.Append(ex.Source);
231+
formattedException.AppendLine("--- SOURCE ---");
232+
formattedException.AppendLine(ex.Source);
259233
}
260234
if (ex.InnerException is not null)
261235
{
262-
formattedException.Append("\n--- INNER ---");
263-
formattedException.Append(ex.InnerException);
236+
formattedException.AppendLine("--- INNER ---");
237+
formattedException.AppendLine(ex.InnerException.ToString());
264238
}
265239
}
266240
else
267241
{
268-
formattedException.Append("\nException is not available.\n");
242+
formattedException.AppendLine("Exception data is not available.");
269243
}
270244

271-
formattedException.Append("---------------------------------------");
245+
formattedException.AppendLine("---------------------------------------");
272246

273247
Debug.WriteLine(formattedException.ToString());
274248

275249
// Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O)
276250
Debugger.Break();
277251

278252
SaveSessionTabs();
279-
App.Logger.LogError(ex, ex?.Message ?? "An error occurred");
253+
App.Logger.LogError(ex, ex?.Message ?? "An unhandled error occurred.");
280254

281255
if (!showToastNotification)
282256
return;

0 commit comments

Comments
 (0)