diff --git a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs index 3a9f5248f07..eb1d5395435 100644 --- a/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs +++ b/src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs @@ -14,6 +14,7 @@ using System.Windows.Media; using System.Windows.Threading; using Dynamo.Configuration; +using Dynamo.Core; using Dynamo.Engine; using Dynamo.Exceptions; using Dynamo.Graph; @@ -22,6 +23,7 @@ using Dynamo.Graph.Nodes; using Dynamo.Graph.Workspaces; using Dynamo.Interfaces; +using Dynamo.Logging; using Dynamo.Models; using Dynamo.PackageManager; using Dynamo.PackageManager.UI; @@ -30,7 +32,6 @@ using Dynamo.Services; using Dynamo.UI; using Dynamo.UI.Prompts; -using Dynamo.Updates; using Dynamo.Utilities; using Dynamo.Visualization; using Dynamo.Wpf.Interfaces; @@ -675,6 +676,8 @@ public struct StartConfiguration protected DynamoViewModel(StartConfiguration startConfiguration) { + Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException; + this.ShowLogin = startConfiguration.ShowLogin; // initialize core data structures @@ -754,6 +757,33 @@ protected DynamoViewModel(StartConfiguration startConfiguration) FileTrustViewModel = new FileTrustWarningViewModel(); } + private void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + if (e.Handled) + { + return; + } + + e.Handled = true; + CrashGracefully(e.Exception); + } + + private void CrashGracefully(Exception ex) + { + try + { + Model?.Logger?.LogError($"Unhandled exception {ex.Message}"); + + DynamoModel.IsCrashing = true; + Analytics.TrackException(ex, true); + Model?.OnRequestsCrashPrompt(new CrashErrorReportArgs(ex)); + + Exit(false); // don't allow cancellation + } + catch + { } + } + /// /// Sets up the provided object and /// adds it to the Watch3DViewModels collection. @@ -3412,6 +3442,8 @@ public ShutdownParams( /// public bool PerformShutdownSequence(ShutdownParams shutdownParams) { + Dispatcher.CurrentDispatcher.UnhandledException -= CurrentDispatcher_UnhandledException; + if (shutdownSequenceInitiated) { // There was a prior call to shutdown. This could happen for example