Skip to content

Commit

Permalink
More rigorous startup failure checking and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jaquadro committed Nov 17, 2013
1 parent b2d54f3 commit 366ec3f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NBTExplorer.Installer/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Product Id="*"
Name="NBTExplorer"
Language="1033"
Version="2.6.0.0"
Version="2.6.1.0"
Manufacturer="Justin Aquadro"
UpgradeCode="0bfb1026-21f2-4552-ad71-ca90aae10a25">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
Expand Down
62 changes: 62 additions & 0 deletions NBTExplorer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using NBTExplorer.Windows;

Expand All @@ -12,6 +16,11 @@ static class Program
[STAThread]
static void Main ()
{
Application.ThreadException += AppThreadFailureHandler;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

AppDomain.CurrentDomain.UnhandledException += AppDomainFailureHandler;

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
Expand All @@ -31,5 +40,58 @@ public static void StaticInitFailure (Exception e)
MessageBox.Show("Application failed during static initialization: " + original.Message);
Application.Exit();
}

private static void AppThreadFailureHandler (object sender, ThreadExceptionEventArgs e)
{
ProcessException(e.Exception);
}

private static void AppDomainFailureHandler (object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is Exception)
ProcessException(e.ExceptionObject as Exception);
else if (e.IsTerminating) {
MessageBox.Show("NBTExplorer encountered an unknown exception object: " + e.ExceptionObject.GetType().FullName,
"NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
}

private static void ProcessException (Exception ex)
{
if (IsMissingSubstrate(ex)) {
MessageBox.Show("NBTExplorer could not find required assembly \"Substrate.dll\".\n\nIf you obtained NBTExplorer from a ZIP distribution, make sure you've extracted NBTExplorer and all of its supporting files into another directory before running it.",
"NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
return;
}

StringBuilder errorText = new StringBuilder();
errorText.AppendLine("NBTExplorer encountered the following exception while trying to run: " + ex.GetType().Name);
errorText.AppendLine("Message: " + ex.Message);

while (ex.InnerException != null) {
ex = ex.InnerException;
errorText.AppendLine();
errorText.AppendLine("Caused by Inner Exception: " + ex.GetType().Name);
errorText.AppendLine("Message: " + ex.Message);
}

MessageBox.Show(errorText.ToString(), "NBTExplorer failed to run", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}

private static bool IsMissingSubstrate (Exception ex)
{
if (ex is TypeInitializationException && ex.InnerException != null)
ex = ex.InnerException;
if (ex is FileNotFoundException) {
FileNotFoundException fileEx = ex as FileNotFoundException;
if (fileEx.FileName.Contains("Substrate"))
return true;
}

return false;
}
}
}
4 changes: 2 additions & 2 deletions NBTExplorer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.6.0.0")]
[assembly: AssemblyFileVersion("2.6.0.0")]
[assembly: AssemblyVersion("2.6.1.0")]
[assembly: AssemblyFileVersion("2.6.1.0")]

0 comments on commit 366ec3f

Please sign in to comment.