From 721edab8bfb8e2017be5319c7fb012ad1d784042 Mon Sep 17 00:00:00 2001 From: Matteo Cominetti Date: Wed, 31 Aug 2022 23:00:54 +0100 Subject: [PATCH] Revit/misc (#1567) * fix: updates some old manager links and paths * feat: check for updates against the new endpoint * feat(revit): adds some safity guards & herror handling --- .../Entry/OneClickSendCommand.cs | 43 +++--- .../Entry/SpeckleRevitCommand2.cs | 129 +++++++++++------- Core/Core/Logging/Log.cs | 113 ++++++++------- .../DesktopUI2/ViewModels/MainViewModel.cs | 16 +-- 4 files changed, 179 insertions(+), 122 deletions(-) diff --git a/ConnectorRevit/ConnectorRevit/Entry/OneClickSendCommand.cs b/ConnectorRevit/ConnectorRevit/Entry/OneClickSendCommand.cs index 8d4ddf76e2..c7194813fd 100644 --- a/ConnectorRevit/ConnectorRevit/Entry/OneClickSendCommand.cs +++ b/ConnectorRevit/ConnectorRevit/Entry/OneClickSendCommand.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using Autodesk.Revit.Attributes; @@ -9,6 +10,7 @@ using DesktopUI2.ViewModels; using DesktopUI2.Views; using Speckle.ConnectorRevit.UI; +using Speckle.Core.Logging; namespace Speckle.ConnectorRevit.Entry { @@ -45,26 +47,24 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme public static void CreateOrFocusSpeckle(bool showWindow = true) { - - if (MainWindow == null) + try { - var viewModel = new MainViewModel(Bindings); - MainWindow = new MainWindow + if (MainWindow == null) { - DataContext = viewModel - }; - - //massive hack: we start the avalonia main loop and stop it immediately (since it's thread blocking) - //to avoid an annoying error when closing revit - var cts = new CancellationTokenSource(); - cts.CancelAfter(100); - AvaloniaApp.Run(cts.Token); - + var viewModel = new MainViewModel(Bindings); + MainWindow = new MainWindow + { + DataContext = viewModel + }; + + //massive hack: we start the avalonia main loop and stop it immediately (since it's thread blocking) + //to avoid an annoying error when closing revit + var cts = new CancellationTokenSource(); + cts.CancelAfter(100); + AvaloniaApp.Run(cts.Token); + } - } - try - { if (showWindow) { MainWindow.Show(); @@ -85,6 +85,17 @@ public static void CreateOrFocusSpeckle(bool showWindow = true) } catch (Exception ex) { + Log.CaptureException(ex, Sentry.SentryLevel.Error); + var td = new TaskDialog("Error"); + td.MainContent = $"Oh no! Something went wrong while loading Speckle, please report it on the forum:\n{ex.Message}"; + td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Report issue on our Community Forum"); + + TaskDialogResult tResult = td.Show(); + + if (TaskDialogResult.CommandLink1 == tResult) + { + Process.Start("https://speckle.community/"); + } } } diff --git a/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs b/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs index 8de7da2f7a..ba2307a8e0 100644 --- a/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs +++ b/ConnectorRevit/ConnectorRevit/Entry/SpeckleRevitCommand2.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using Autodesk.Revit.Attributes; @@ -76,75 +77,92 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme internal static void RegisterPane() { - if (!UseDockablePanel) - return; + try + { + if (!UseDockablePanel) + return; - var registered = DockablePane.PaneIsRegistered(PanelId); - var created = DockablePane.PaneExists(PanelId); + var registered = DockablePane.PaneIsRegistered(PanelId); + var created = DockablePane.PaneExists(PanelId); - if (registered && created) - { - _panel.Init(); - return; - } + if (registered && created) + { + _panel.Init(); + return; + } - if (!registered) - { - //Register dockable panel - var viewModel = new MainViewModel(Bindings); - _panel = new Panel + if (!registered) { - DataContext = viewModel - }; - App.AppInstance.RegisterDockablePane(PanelId, "Speckle", _panel); - _panel.Init(); - } - created = DockablePane.PaneExists(PanelId); + //Register dockable panel + var viewModel = new MainViewModel(Bindings); + _panel = new Panel + { + DataContext = viewModel + }; + App.AppInstance.RegisterDockablePane(PanelId, "Speckle", _panel); + _panel.Init(); + } + created = DockablePane.PaneExists(PanelId); + + //if revit was launched double-clicking on a Revit file, we're screwed + //could maybe show the old window? + if (!created && App.AppInstance.Application.Documents.Size > 0) + { + TaskDialog mainDialog = new TaskDialog("Dockable Panel Issue"); + mainDialog.MainInstruction = "Dockable Panel Issue"; + mainDialog.MainContent = + "Revit cannot properly register Dockable Panels when launched by double-clicking a Revit file. " + + "Please close and re-open Revit without launching a file OR open/create a new project to trigger the Speckle panel registration."; - //if revit was launched double-clicking on a Revit file, we're screwed - //could maybe show the old window? - if (!created && App.AppInstance.Application.Documents.Size > 0) - { - TaskDialog mainDialog = new TaskDialog("Dockable Panel Issue"); - mainDialog.MainInstruction = "Dockable Panel Issue"; - mainDialog.MainContent = - "Revit cannot properly register Dockable Panels when launched by double-clicking a Revit file. " - + "Please close and re-open Revit without launching a file OR open/create a new project to trigger the Speckle panel registration."; + // Set footer text. Footer text is usually used to link to the help document. + mainDialog.FooterText = + "" + + "Click here for more info"; - // Set footer text. Footer text is usually used to link to the help document. - mainDialog.FooterText = - "" - + "Click here for more info"; + mainDialog.Show(); + + } + } + catch (Exception ex) + { + Log.CaptureException(ex, Sentry.SentryLevel.Error); + var td = new TaskDialog("Error"); + td.MainContent = $"Oh no! Something went wrong while loading Speckle, please report it on the forum:\n{ex.Message}"; + td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Report issue on our Community Forum"); - mainDialog.Show(); + TaskDialogResult tResult = td.Show(); + if (TaskDialogResult.CommandLink1 == tResult) + { + Process.Start("https://speckle.community/"); + } } } public static void CreateOrFocusSpeckle(bool showWindow = true) { - - - if (MainWindow == null) + try { - var viewModel = new MainViewModel(Bindings); - MainWindow = new MainWindow + + if (MainWindow == null) { - DataContext = viewModel - }; + var viewModel = new MainViewModel(Bindings); + MainWindow = new MainWindow + { + DataContext = viewModel + }; - //massive hack: we start the avalonia main loop and stop it immediately (since it's thread blocking) - //to avoid an annoying error when closing revit - var cts = new CancellationTokenSource(); - cts.CancelAfter(100); - AvaloniaApp.Run(cts.Token); + //massive hack: we start the avalonia main loop and stop it immediately (since it's thread blocking) + //to avoid an annoying error when closing revit + var cts = new CancellationTokenSource(); + cts.CancelAfter(100); + AvaloniaApp.Run(cts.Token); + + } - } - try - { if (showWindow) { MainWindow.Show(); @@ -165,6 +183,17 @@ public static void CreateOrFocusSpeckle(bool showWindow = true) } catch (Exception ex) { + Log.CaptureException(ex, Sentry.SentryLevel.Error); + var td = new TaskDialog("Error"); + td.MainContent = $"Oh no! Something went wrong while loading Speckle, please report it on the forum:\n{ex.Message}"; + td.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "Report issue on our Community Forum"); + + TaskDialogResult tResult = td.Show(); + + if (TaskDialogResult.CommandLink1 == tResult) + { + Process.Start("https://speckle.community/"); + } } } diff --git a/Core/Core/Logging/Log.cs b/Core/Core/Logging/Log.cs index df1192df92..ffd4f84924 100644 --- a/Core/Core/Logging/Log.cs +++ b/Core/Core/Logging/Log.cs @@ -17,73 +17,92 @@ public static class Log public static void Initialize() { - if (_initialized) - return; + try + { + if (_initialized) + return; - var dsn = "https://f29ec716d14d4121bb2a71c4f3ef7786@o436188.ingest.sentry.io/5396846"; + var dsn = "https://f29ec716d14d4121bb2a71c4f3ef7786@o436188.ingest.sentry.io/5396846"; - var env = "production"; - var debug = false; + var env = "production"; + var debug = false; #if DEBUG - env = "dev"; - dsn = null; - debug = true; + env = "dev"; + dsn = null; + debug = true; #endif - SentrySdk.Init(o => - { - o.Dsn = dsn; - o.Environment = env; - o.Debug = debug; - o.Release = "SpeckleCore@" + Assembly.GetExecutingAssembly().GetName().Version.ToString(); - o.StackTraceMode = StackTraceMode.Enhanced; - o.AttachStacktrace = true; - }); + SentrySdk.Init(o => + { + o.Dsn = dsn; + o.Environment = env; + o.Debug = debug; + o.Release = "SpeckleCore@" + Assembly.GetExecutingAssembly().GetName().Version.ToString(); + o.StackTraceMode = StackTraceMode.Enhanced; + o.AttachStacktrace = true; + }); - var da = AccountManager.GetDefaultAccount(); - var id = da != null ? da.GetHashedEmail() : "unknown"; + var da = AccountManager.GetDefaultAccount(); + var id = da != null ? da.GetHashedEmail() : "unknown"; - SentrySdk.ConfigureScope(scope => - { - scope.User = new User { Id = id, }; - scope.SetTag("hostApplication", Setup.HostApplication); - }); + SentrySdk.ConfigureScope(scope => + { + scope.User = new User { Id = id, }; + scope.SetTag("hostApplication", Setup.HostApplication); + }); - _initialized = true; + _initialized = true; + } + catch (Exception ex) + { + //swallow + } } //capture and make sure Sentry is initialized - public static void CaptureException( - Exception e, - SentryLevel level = SentryLevel.Info, - List> extra = null) + public static void CaptureException(Exception e, SentryLevel level = SentryLevel.Info, List> extra = null) { - Initialize(); - - //ignore infos as they're hogging us - if (level == SentryLevel.Info) - return; - - SentrySdk.WithScope(s => + try + { + Initialize(); + + //ignore infos as they're hogging us + if (level == SentryLevel.Info) + return; + + SentrySdk.WithScope(s => + { + s.Level = level; + + if (extra != null) + s.SetExtras(extra); + if (e is AggregateException aggregate) + aggregate.InnerExceptions.ToList().ForEach(ex => SentrySdk.CaptureException(e)); + else + SentrySdk.CaptureException(e); + }); + + } + catch (Exception ex) { - s.Level = level; - - if (extra != null) - s.SetExtras(extra); - if (e is AggregateException aggregate) - aggregate.InnerExceptions.ToList().ForEach(ex => SentrySdk.CaptureException(e)); - else - SentrySdk.CaptureException(e); - }); + //swallow + } } public static void AddBreadcrumb(string message) { - Initialize(); - SentrySdk.AddBreadcrumb(message); + try + { + Initialize(); + SentrySdk.AddBreadcrumb(message); + } + catch (Exception ex) + { + //swallow + } } } } diff --git a/DesktopUI2/DesktopUI2/ViewModels/MainViewModel.cs b/DesktopUI2/DesktopUI2/ViewModels/MainViewModel.cs index a819ed6540..5a3e628623 100644 --- a/DesktopUI2/DesktopUI2/ViewModels/MainViewModel.cs +++ b/DesktopUI2/DesktopUI2/ViewModels/MainViewModel.cs @@ -26,7 +26,7 @@ public class MainViewModel : ViewModelBase, IScreen public bool DialogVisible { - get => _dialogBody!=null; + get => _dialogBody != null; } public double DialogOpacity @@ -38,7 +38,8 @@ public double DialogOpacity public UserControl DialogBody { get => _dialogBody; - set { + set + { this.RaiseAndSetIfChanged(ref _dialogBody, value); this.RaisePropertyChanged("DialogVisible"); @@ -55,12 +56,13 @@ public MainViewModel(ConnectorBindings _bindings) } public MainViewModel() { - + Init(); } private void Init() { + Instance = this; Setup.Init(Bindings.GetHostAppNameVersion(), Bindings.GetHostAppName()); @@ -80,19 +82,15 @@ private void Init() Bindings.UpdateSavedStreams = HomeViewModel.Instance.UpdateSavedStreams; Bindings.UpdateSelectedStream = HomeViewModel.Instance.UpdateSelectedStream; - Router.PropertyChanged += Router_PropertyChanged; + } //https://github.com/AvaloniaUI/Avalonia/issues/5290 private void CatchReactiveException(Exception e) { - //do nothing + Log.CaptureException(e, Sentry.SentryLevel.Error); } - private void Router_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) - { - throw new System.NotImplementedException(); - } public static void GoHome() {