Browse Source

#1481: Improve exception handling in case of MEF initialization errors.

pull/1515/head
Siegfried Pammer 6 years ago
parent
commit
286353984e
  1. 29
      ILSpy/App.xaml.cs
  2. 21
      ILSpy/MainWindow.xaml.cs

29
ILSpy/App.xaml.cs

@ -32,6 +32,7 @@ using ICSharpCode.ILSpy.TextView; @@ -32,6 +32,7 @@ using ICSharpCode.ILSpy.TextView;
using ICSharpCode.ILSpy.Options;
using Microsoft.VisualStudio.Composition;
using System.Text;
namespace ICSharpCode.ILSpy
{
@ -71,7 +72,13 @@ namespace ICSharpCode.ILSpy @@ -71,7 +72,13 @@ namespace ICSharpCode.ILSpy
}
}
InitializeComponent();
if (!System.Diagnostics.Debugger.IsAttached) {
AppDomain.CurrentDomain.UnhandledException += ShowErrorBox;
Dispatcher.CurrentDispatcher.UnhandledException += Dispatcher_UnhandledException;
}
TaskScheduler.UnobservedTaskException += DotNet40_UnobservedTaskException;
// Cannot show MessageBox here, because WPF would crash with a XamlParseException
// Remember and show exceptions in text output, once MainWindow is properly initialized
try {
@ -107,16 +114,10 @@ namespace ICSharpCode.ILSpy @@ -107,16 +114,10 @@ namespace ICSharpCode.ILSpy
// This throws exceptions for composition failures. Alternatively, the configuration's CompositionErrors property
// could be used to log the errors directly. Used at the end so that it does not prevent the export provider setup.
config.ThrowOnErrors();
}
catch (Exception ex) {
} catch (Exception ex) {
StartupExceptions.Add(new ExceptionData { Exception = ex });
}
if (!System.Diagnostics.Debugger.IsAttached) {
AppDomain.CurrentDomain.UnhandledException += ShowErrorBox;
Dispatcher.CurrentDispatcher.UnhandledException += Dispatcher_UnhandledException;
}
TaskScheduler.UnobservedTaskException += DotNet40_UnobservedTaskException;
Languages.Initialize(exportProvider);
EventManager.RegisterClassHandler(typeof(Window),
@ -124,7 +125,17 @@ namespace ICSharpCode.ILSpy @@ -124,7 +125,17 @@ namespace ICSharpCode.ILSpy
new RequestNavigateEventHandler(Window_RequestNavigate));
ILSpyTraceListener.Install();
}
protected override void OnStartup(StartupEventArgs e)
{
var output = new StringBuilder();
if (ILSpy.MainWindow.FormatExceptions(StartupExceptions.ToArray(), output)) {
MessageBox.Show(output.ToString(), "Sorry we crashed!");
Environment.Exit(1);
}
base.OnStartup(e);
}
string FullyQualifyPath(string argument)
{
// Fully qualify the paths before passing them to another process,

21
ILSpy/MainWindow.xaml.cs

@ -24,6 +24,7 @@ using System.Diagnostics; @@ -24,6 +24,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@ -451,6 +452,16 @@ namespace ICSharpCode.ILSpy @@ -451,6 +452,16 @@ namespace ICSharpCode.ILSpy
}
bool FormatExceptions(App.ExceptionData[] exceptions, ITextOutput output)
{
var stringBuilder = new StringBuilder();
var result = FormatExceptions(exceptions, stringBuilder);
if (result) {
output.Write(stringBuilder.ToString());
}
return result;
}
internal static bool FormatExceptions(App.ExceptionData[] exceptions, StringBuilder output)
{
if (exceptions.Length == 0) return false;
bool first = true;
@ -459,16 +470,16 @@ namespace ICSharpCode.ILSpy @@ -459,16 +470,16 @@ namespace ICSharpCode.ILSpy
if (first)
first = false;
else
output.WriteLine("-------------------------------------------------");
output.WriteLine("Error(s) loading plugin: " + item.PluginName);
output.AppendLine("-------------------------------------------------");
output.AppendLine("Error(s) loading plugin: " + item.PluginName);
if (item.Exception is System.Reflection.ReflectionTypeLoadException) {
var e = (System.Reflection.ReflectionTypeLoadException)item.Exception;
foreach (var ex in e.LoaderExceptions) {
output.WriteLine(ex.ToString());
output.WriteLine();
output.AppendLine(ex.ToString());
output.AppendLine();
}
} else
output.WriteLine(item.Exception.ToString());
output.AppendLine(item.Exception.ToString());
}
return true;

Loading…
Cancel
Save