@@ -22,26 +22,25 @@ public static void Initialize(IServiceProvider serviceProvider, Application app)
2222 serviceProvider . GetRequiredService < ExceptionHandlingSupport > ( ) . Attach ( app ) ;
2323 }
2424
25+ /// <summary>
26+ /// Kill the current process if the exception is or has a DbException.
27+ /// As this method does not throw, it should only be used in catch blocks
28+ /// </summary>
29+ /// <param name="exception">Incoming exception</param>
30+ /// <returns>Unwrapped DbException or original exception</returns>
2531 [ StackTraceHidden ]
26- public static void KillProcessOnDbException ( Exception exception )
32+ public static Exception KillProcessOnDbExceptionNoThrow ( Exception exception )
2733 {
28- ExceptionDispatchInfo dispatch = ExceptionDispatchInfo . Capture ( exception ) ;
29-
30- switch ( dispatch . SourceException )
34+ return exception switch
3135 {
32- case DbException dbException :
33- throw KillProcessOnDbException ( dbException ) ;
34- case DbUpdateException { InnerException : DbException dbException2 } :
35- throw KillProcessOnDbException ( dbException2 ) ;
36- default :
37- // In case it's not a DbException, we should preserve the original stack trace
38- dispatch . Throw ( ) ;
39- break ;
40- }
36+ DbException dbException => KillProcessOnDbException ( dbException ) ,
37+ DbUpdateException { InnerException : DbException dbException2 } => KillProcessOnDbException ( dbException2 ) ,
38+ _ => exception ,
39+ } ;
4140 }
4241
4342 [ StackTraceHidden ]
44- public static DbException KillProcessOnDbException ( DbException exception )
43+ private static DbException KillProcessOnDbException ( DbException exception )
4544 {
4645 HutaoNative . Instance . ShowErrorMessage ( "Warning | 警告" , exception . Message ) ;
4746 Process . GetCurrentProcess ( ) . Kill ( ) ;
@@ -60,7 +59,7 @@ private static void OnAppUnhandledException(object? sender, Microsoft.UI.Xaml.Un
6059 Debugger . Break ( ) ;
6160 XamlApplicationLifetime . Exiting = true ;
6261
63- KillProcessOnDbException ( e . Exception ) ;
62+ KillProcessOnDbExceptionNoThrow ( e . Exception ) ;
6463
6564 // https://github.com/getsentry/sentry-dotnet/blob/main/src/Sentry/Integrations/WinUIUnhandledExceptionIntegration.cs
6665 exception . SetSentryMechanism ( "Microsoft.UI.Xaml.UnhandledException" , handled : false ) ;
0 commit comments