Skip to content

Commit a9fa019

Browse files
committed
Add better exception handling.
1 parent 727ac06 commit a9fa019

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

src/StructuredLogViewer/ExceptionHandler.cs

+29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Reflection;
23
using Microsoft.Build.Logging.StructuredLogger;
34

45
namespace StructuredLogViewer
@@ -14,5 +15,33 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
1415
{
1516
ErrorReporting.ReportException(e.ExceptionObject as Exception);
1617
}
18+
19+
public static Exception Unwrap(Exception ex)
20+
{
21+
if (ex is ReflectionTypeLoadException reflectionTypeLoadException)
22+
{
23+
if (reflectionTypeLoadException.LoaderExceptions != null && reflectionTypeLoadException.LoaderExceptions.Length > 0)
24+
{
25+
return Unwrap(reflectionTypeLoadException.LoaderExceptions[0]);
26+
}
27+
}
28+
29+
if (ex is TargetInvocationException tie)
30+
{
31+
return Unwrap(tie.InnerException);
32+
}
33+
34+
if (ex is AggregateException ae)
35+
{
36+
return Unwrap(ae.Flatten().InnerExceptions[0]);
37+
}
38+
39+
if (ex.InnerException != null)
40+
{
41+
return Unwrap(ex.InnerException);
42+
}
43+
44+
return ex;
45+
}
1746
}
1847
}

src/StructuredLogViewer/HostedBuild.cs

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public Task<Build> BuildAndGetResult(BuildProgress progress)
6767
}
6868
catch (Exception ex)
6969
{
70+
ex = ExceptionHandler.Unwrap(ex);
7071
var build = new Build();
7172
build.Succeeded = false;
7273
build.AddChild(new Message() { Text = "Exception occurred during build:" });

src/StructuredLogViewer/MainWindow.xaml.cs

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ private async void OpenLogFile(string filePath)
239239
}
240240
catch (Exception ex)
241241
{
242+
ex = ExceptionHandler.Unwrap(ex);
242243
shouldAnalyze = false;
243244
return GetErrorBuild(filePath, ex.ToString());
244245
}

src/StructuredLogger/Serialization/Serialization.cs

+18-7
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,24 @@ public class Serialization
1919
.GetFields(BindingFlags.Public | BindingFlags.Static)
2020
.Select(f => f.Name).ToArray();
2121

22-
public static readonly Dictionary<string, Type> ObjectModelTypes =
23-
typeof(TreeNode)
24-
.GetTypeInfo()
25-
.Assembly
26-
.GetTypes()
27-
.Where(t => typeof(BaseNode).IsAssignableFrom(t))
28-
.ToDictionary(t => t.Name);
22+
private static Dictionary<string, Type> objectModelTypes;
23+
public static Dictionary<string, Type> ObjectModelTypes
24+
{
25+
get
26+
{
27+
if (objectModelTypes == null)
28+
{
29+
objectModelTypes = typeof(TreeNode)
30+
.GetTypeInfo()
31+
.Assembly
32+
.GetTypes()
33+
.Where(t => typeof(BaseNode).IsAssignableFrom(t))
34+
.ToDictionary(t => t.Name);
35+
}
36+
37+
return objectModelTypes;
38+
}
39+
}
2940

3041
public static Build Read(string filePath)
3142
{

0 commit comments

Comments
 (0)