Skip to content

Commit

Permalink
Add better exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillOsenkov committed Mar 20, 2017
1 parent 727ac06 commit a9fa019
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
29 changes: 29 additions & 0 deletions src/StructuredLogViewer/ExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;
using Microsoft.Build.Logging.StructuredLogger;

namespace StructuredLogViewer
Expand All @@ -14,5 +15,33 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
{
ErrorReporting.ReportException(e.ExceptionObject as Exception);
}

public static Exception Unwrap(Exception ex)
{
if (ex is ReflectionTypeLoadException reflectionTypeLoadException)
{
if (reflectionTypeLoadException.LoaderExceptions != null && reflectionTypeLoadException.LoaderExceptions.Length > 0)
{
return Unwrap(reflectionTypeLoadException.LoaderExceptions[0]);
}
}

if (ex is TargetInvocationException tie)
{
return Unwrap(tie.InnerException);
}

if (ex is AggregateException ae)
{
return Unwrap(ae.Flatten().InnerExceptions[0]);
}

if (ex.InnerException != null)
{
return Unwrap(ex.InnerException);
}

return ex;
}
}
}
1 change: 1 addition & 0 deletions src/StructuredLogViewer/HostedBuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public Task<Build> BuildAndGetResult(BuildProgress progress)
}
catch (Exception ex)
{
ex = ExceptionHandler.Unwrap(ex);
var build = new Build();
build.Succeeded = false;
build.AddChild(new Message() { Text = "Exception occurred during build:" });
Expand Down
1 change: 1 addition & 0 deletions src/StructuredLogViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ private async void OpenLogFile(string filePath)
}
catch (Exception ex)
{
ex = ExceptionHandler.Unwrap(ex);
shouldAnalyze = false;
return GetErrorBuild(filePath, ex.ToString());
}
Expand Down
25 changes: 18 additions & 7 deletions src/StructuredLogger/Serialization/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ public class Serialization
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(f => f.Name).ToArray();

public static readonly Dictionary<string, Type> ObjectModelTypes =
typeof(TreeNode)
.GetTypeInfo()
.Assembly
.GetTypes()
.Where(t => typeof(BaseNode).IsAssignableFrom(t))
.ToDictionary(t => t.Name);
private static Dictionary<string, Type> objectModelTypes;
public static Dictionary<string, Type> ObjectModelTypes
{
get
{
if (objectModelTypes == null)
{
objectModelTypes = typeof(TreeNode)
.GetTypeInfo()
.Assembly
.GetTypes()
.Where(t => typeof(BaseNode).IsAssignableFrom(t))
.ToDictionary(t => t.Name);
}

return objectModelTypes;
}
}

public static Build Read(string filePath)
{
Expand Down

0 comments on commit a9fa019

Please sign in to comment.