Skip to content

Commit f7ec0d8

Browse files
DYN-6492 add unhandled exception handler to viewmodel (#14840)
* add unhandled exception handler to viewmodel * cleanup * add backup crash dialog if CER is unavailable * catch any exceptions thrown from handler itself
1 parent d687a0f commit f7ec0d8

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

src/DynamoCoreWpf/ViewModels/Core/DynamoViewModel.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Windows.Media;
1515
using System.Windows.Threading;
1616
using Dynamo.Configuration;
17+
using Dynamo.Core;
1718
using Dynamo.Engine;
1819
using Dynamo.Exceptions;
1920
using Dynamo.Graph;
@@ -22,6 +23,7 @@
2223
using Dynamo.Graph.Nodes;
2324
using Dynamo.Graph.Workspaces;
2425
using Dynamo.Interfaces;
26+
using Dynamo.Logging;
2527
using Dynamo.Models;
2628
using Dynamo.PackageManager;
2729
using Dynamo.PackageManager.UI;
@@ -30,7 +32,6 @@
3032
using Dynamo.Services;
3133
using Dynamo.UI;
3234
using Dynamo.UI.Prompts;
33-
using Dynamo.Updates;
3435
using Dynamo.Utilities;
3536
using Dynamo.Visualization;
3637
using Dynamo.Wpf.Interfaces;
@@ -675,6 +676,8 @@ public struct StartConfiguration
675676

676677
protected DynamoViewModel(StartConfiguration startConfiguration)
677678
{
679+
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
680+
678681
this.ShowLogin = startConfiguration.ShowLogin;
679682

680683
// initialize core data structures
@@ -754,6 +757,33 @@ protected DynamoViewModel(StartConfiguration startConfiguration)
754757
FileTrustViewModel = new FileTrustWarningViewModel();
755758
}
756759

760+
private void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
761+
{
762+
if (e.Handled)
763+
{
764+
return;
765+
}
766+
767+
e.Handled = true;
768+
CrashGracefully(e.Exception);
769+
}
770+
771+
private void CrashGracefully(Exception ex)
772+
{
773+
try
774+
{
775+
Model?.Logger?.LogError($"Unhandled exception {ex.Message}");
776+
777+
DynamoModel.IsCrashing = true;
778+
Analytics.TrackException(ex, true);
779+
Model?.OnRequestsCrashPrompt(new CrashErrorReportArgs(ex));
780+
781+
Exit(false); // don't allow cancellation
782+
}
783+
catch
784+
{ }
785+
}
786+
757787
/// <summary>
758788
/// Sets up the provided <see cref="DefaultWatch3DViewModel"/> object and
759789
/// adds it to the Watch3DViewModels collection.
@@ -3412,6 +3442,8 @@ public ShutdownParams(
34123442
///
34133443
public bool PerformShutdownSequence(ShutdownParams shutdownParams)
34143444
{
3445+
Dispatcher.CurrentDispatcher.UnhandledException -= CurrentDispatcher_UnhandledException;
3446+
34153447
if (shutdownSequenceInitiated)
34163448
{
34173449
// There was a prior call to shutdown. This could happen for example

0 commit comments

Comments
 (0)