Skip to content

Commit

Permalink
Report inner exception details if available. (#2357)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcfanning committed May 17, 2021
1 parent 1d59810 commit b125bd2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Sarif/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public static Notification CreateNotification(
string message = string.Format(CultureInfo.CurrentCulture, messageFormat, args);

ExceptionData exceptionData = exception != null && persistExceptionStack
? ExceptionData.Create(exception)
? ExceptionData.Create(exception.InnerException ?? exception)
: null;

PhysicalLocation physicalLocation = uri != null
Expand Down
18 changes: 18 additions & 0 deletions src/Test.UnitTests.Sarif.Driver/Sdk/AnalyzeCommandBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,24 @@ public void ExceptionRaisedInvokingAnalyze()
);
}


[Fact]
public void ExceptionRaisedInvokingAnalyze_PersistInnerException()
{
string location = GetThisTestAssemblyFilePath();

Run run = AnalyzeFile(location,
TestRuleBehaviors.RaiseExceptionInvokingAnalyze,
runtimeConditions: RuntimeConditions.ExceptionInSkimmerAnalyze,
expectedReturnCode: 1);

run.Invocations[0]?.ToolExecutionNotifications.Count.Should().Be(1);
Stack stack = run.Invocations[0]?.ToolExecutionNotifications[0].Exception.Stack;
string fqn = stack.Frames[0].Location.LogicalLocation.FullyQualifiedName;
fqn.Contains(nameof(TestRule.RaiseExceptionViaReflection)).Should().BeTrue();
}


[Fact]
public void ExceptionRaisedInEngine()
{
Expand Down
10 changes: 9 additions & 1 deletion src/Test.UnitTests.Sarif.Driver/TestRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Reflection;
using System.Resources;

using FluentAssertions;
Expand Down Expand Up @@ -148,7 +149,9 @@ public override void Analyze(TestAnalysisContext context)
{
case TestRuleBehaviors.RaiseExceptionInvokingAnalyze:
{
throw new InvalidOperationException(nameof(TestRuleBehaviors.RaiseExceptionInvokingAnalyze));
MethodInfo mi = this.GetType().GetMethod("RaiseExceptionViaReflection");
mi.Invoke(null, new object[] { });
break;
}

case TestRuleBehaviors.RaiseTargetParseError:
Expand Down Expand Up @@ -235,6 +238,11 @@ public override void Analyze(TestAnalysisContext context)
}
}

public static void RaiseExceptionViaReflection()
{
throw new InvalidOperationException(nameof(TestRuleBehaviors.RaiseExceptionInvokingAnalyze));
}

public IEnumerable<IOption> GetOptions()
{
return new IOption[] { Behaviors, UnusedOption };
Expand Down

0 comments on commit b125bd2

Please sign in to comment.