@@ -250,94 +250,99 @@ public static void SaveSessionTabs()
250
250
/// </summary>
251
251
public static void HandleAppUnhandledException ( Exception ? ex , bool showToastNotification )
252
252
{
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 ( ( ) =>
263
254
{
264
- ex . Data [ Mechanism . HandledKey ] = false ;
265
- ex . Data [ Mechanism . MechanismKey ] = "Application.UnhandledException" ;
255
+ var generalSettingsService = Ioc . Default . GetRequiredService < IGeneralSettingsService > ( ) ;
266
256
267
- SentrySdk . CaptureException ( ex , scope =>
257
+ StringBuilder formattedException = new ( )
268
258
{
269
- scope . User . Id = generalSettingsService ? . UserId ;
270
- scope . Level = SentryLevel . Fatal ;
271
- } ) ;
259
+ Capacity = 200
260
+ } ;
272
261
273
- formattedException . AppendLine ( $ ">>>> HRESULT: { ex . HResult } ") ;
262
+ formattedException . AppendLine ( "--------- UNHANDLED EXCEPTION --------- ") ;
274
263
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 )
281
265
{
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
+ }
284
297
}
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
291
299
{
292
- formattedException . AppendLine ( "--- INNER ---" ) ;
293
- formattedException . AppendLine ( ex . InnerException . ToString ( ) ) ;
300
+ formattedException . AppendLine ( "Exception data is not available." ) ;
294
301
}
295
- }
296
- else
297
- {
298
- formattedException . AppendLine ( "Exception data is not available." ) ;
299
- }
300
302
301
- formattedException . AppendLine ( "---------------------------------------" ) ;
303
+ formattedException . AppendLine ( "---------------------------------------" ) ;
302
304
303
- Debug . WriteLine ( formattedException . ToString ( ) ) ;
305
+ Debug . WriteLine ( formattedException . ToString ( ) ) ;
304
306
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
+ } ) ;
307
312
308
313
// 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 ) ;
311
315
312
316
if ( ! showToastNotification )
313
317
return ;
314
318
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 ) ;
323
320
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 ( ( ) =>
330
322
{
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 ;
333
326
334
- // Try to re-launch and start over
335
- MainWindow . Instance . DispatcherQueue . EnqueueOrInvokeAsync ( async ( ) =>
327
+ if ( userSettingsService . GeneralSettingsService . LastCrashedTabList ? . SequenceEqual ( lastSessionTabList ) ?? false )
336
328
{
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
+
341
346
Process . GetCurrentProcess ( ) . Kill ( ) ;
342
347
}
343
348
0 commit comments