From 4fd16f04e06f144a1644c074ddbf6f9dbc36d000 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Thu, 23 Sep 2021 09:19:35 -0400 Subject: [PATCH] More robust error handling, especially before logging can be initialized --- AppScaffolding/AppScaffolding.csproj | 2 +- AppScaffolding/UNSAFE_MigrationHelper.cs | 22 +++++- .../Dialogs/MessageBoxAlertAdminDialog.cs | 31 ++++++++- LibationWinForms/MessageBoxAlertAdmin.cs | 11 +-- LibationWinForms/Program.cs | 68 ++++++++++++------- 5 files changed, 100 insertions(+), 34 deletions(-) diff --git a/AppScaffolding/AppScaffolding.csproj b/AppScaffolding/AppScaffolding.csproj index 5c9edfa3..4a8a083d 100644 --- a/AppScaffolding/AppScaffolding.csproj +++ b/AppScaffolding/AppScaffolding.csproj @@ -3,7 +3,7 @@ net5.0 - 6.0.3.1 + 6.0.4.1 diff --git a/AppScaffolding/UNSAFE_MigrationHelper.cs b/AppScaffolding/UNSAFE_MigrationHelper.cs index a0f937c8..cd5e332b 100644 --- a/AppScaffolding/UNSAFE_MigrationHelper.cs +++ b/AppScaffolding/UNSAFE_MigrationHelper.cs @@ -61,7 +61,16 @@ namespace AppScaffolding return; var startingContents = File.ReadAllText(APPSETTINGS_JSON); - var jObj = JObject.Parse(startingContents); + + JObject jObj; + try + { + jObj = JObject.Parse(startingContents); + } + catch + { + return; + } action(jObj); @@ -130,7 +139,16 @@ namespace AppScaffolding return; var startingContents = File.ReadAllText(SettingsJsonPath); - var jObj = JObject.Parse(startingContents); + + JObject jObj; + try + { + jObj = JObject.Parse(startingContents); + } + catch + { + return; + } action(jObj); diff --git a/LibationWinForms/Dialogs/MessageBoxAlertAdminDialog.cs b/LibationWinForms/Dialogs/MessageBoxAlertAdminDialog.cs index c4b44d92..6bb97d7c 100644 --- a/LibationWinForms/Dialogs/MessageBoxAlertAdminDialog.cs +++ b/LibationWinForms/Dialogs/MessageBoxAlertAdminDialog.cs @@ -33,10 +33,37 @@ namespace LibationWinForms.Dialogs } private void githubLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) - => Go.To.Url("https://github.com/rmcrackan/Libation/issues"); + { + var url = "https://github.com/rmcrackan/Libation/issues"; + try + { + Go.To.Url(url); + } + catch + { + MessageBox.Show($"Error opening url\r\n{url}", "Error opening url", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } private void logsLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) - => Go.To.Folder(FileManager.Configuration.Instance.LibationFiles); + { + string dir = ""; + try + { + dir = FileManager.Configuration.Instance.LibationFiles; + } + catch { } + + try + { + Go.To.Folder(dir); + } + catch + { + MessageBox.Show($"Error opening folder\r\n{dir}", "Error opening folder", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void okBtn_Click(object sender, EventArgs e) { diff --git a/LibationWinForms/MessageBoxAlertAdmin.cs b/LibationWinForms/MessageBoxAlertAdmin.cs index a1423a7e..3d68836f 100644 --- a/LibationWinForms/MessageBoxAlertAdmin.cs +++ b/LibationWinForms/MessageBoxAlertAdmin.cs @@ -1,6 +1,5 @@ -using LibationWinForms.Dialogs; -using System; -using System.Linq; +using System; +using LibationWinForms.Dialogs; namespace LibationWinForms { @@ -15,7 +14,11 @@ namespace LibationWinForms /// One of the System.Windows.Forms.DialogResult values. public static System.Windows.Forms.DialogResult Show(string text, string caption, Exception exception) { - Serilog.Log.Logger.Error(exception, "Alert admin error: {@DebugText}", new { text, caption }); + try + { + Serilog.Log.Logger.Error(exception, "Alert admin error: {@DebugText}", new { text, caption }); + } + catch { } using var form = new MessageBoxAlertAdminDialog(text, caption, exception); return form.ShowDialog(); diff --git a/LibationWinForms/Program.cs b/LibationWinForms/Program.cs index cb9bf0ef..80be122a 100644 --- a/LibationWinForms/Program.cs +++ b/LibationWinForms/Program.cs @@ -24,36 +24,54 @@ namespace LibationWinForms [STAThread] static void Main() { - //// Uncomment to see Console. Must be called before anything writes to Console. - //// Only use while debugging. Acts erratically in the wild - //AllocConsole(); + try + { + //// Uncomment to see Console. Must be called before anything writes to Console. + //// Only use while debugging. Acts erratically in the wild + //AllocConsole(); - Application.SetHighDpiMode(HighDpiMode.SystemAware); - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); + Application.SetHighDpiMode(HighDpiMode.SystemAware); + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); - //***********************************************// - // // - // do not use Configuration before this line // - // // - //***********************************************// - // Migrations which must occur before configuration is loaded for the first time. Usually ones which alter the Configuration - var config = AppScaffolding.LibationScaffolding.RunPreConfigMigrations(); + //***********************************************// + // // + // do not use Configuration before this line // + // // + //***********************************************// + // Migrations which must occur before configuration is loaded for the first time. Usually ones which alter the Configuration + var config = AppScaffolding.LibationScaffolding.RunPreConfigMigrations(); - RunInstaller(config); + RunInstaller(config); - // most migrations go in here - AppScaffolding.LibationScaffolding.RunPostConfigMigrations(); + // most migrations go in here + AppScaffolding.LibationScaffolding.RunPostConfigMigrations(); - // migrations which require Forms or are long-running - RunWindowsOnlyMigrations(config); + // migrations which require Forms or are long-running + RunWindowsOnlyMigrations(config); - MessageBoxVerboseLoggingWarning.ShowIfTrue(); + MessageBoxVerboseLoggingWarning.ShowIfTrue(); #if !DEBUG checkForUpdate(); #endif + } + catch (Exception ex) + { + var title = "Fatal error, pre-logging"; + var body = "An unrecoverable error occurred. Since this error happened before logging could be initialized, this error can not be written to the log file."; + try + { + MessageBoxAlertAdmin.Show(body, title, ex); + } + catch + { + MessageBox.Show($"{body}\r\n\r\n{ex.Message}\r\n\r\n{ex.StackTrace}", title, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + return; + } + AppScaffolding.LibationScaffolding.RunPostMigrationScaffolding(); Application.Run(new Form1()); @@ -300,14 +318,14 @@ namespace LibationWinForms if (result != DialogResult.Yes) return; - using var fileSelector = new SaveFileDialog { FileName = zipName, Filter = "Zip Files (*.zip)|*.zip|All files (*.*)|*.*" }; - if (fileSelector.ShowDialog() != DialogResult.OK) - return; - var selectedPath = fileSelector.FileName; - try { - LibationWinForms.BookLiberation.ProcessorAutomationController.DownloadFile(zipUrl, selectedPath, true); + using var fileSelector = new SaveFileDialog { FileName = zipName, Filter = "Zip Files (*.zip)|*.zip|All files (*.*)|*.*" }; + if (fileSelector.ShowDialog() != DialogResult.OK) + return; + var selectedPath = fileSelector.FileName; + + BookLiberation.ProcessorAutomationController.DownloadFile(zipUrl, selectedPath, true); } catch (Exception ex) {