Skip to content

Commit

Permalink
Fixes #2556: Xunit.Sdk.MultipleException Empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jul 25, 2022
1 parent 7eba502 commit 42307e8
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/common/ExceptionUtility.cs
@@ -1,5 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit.Abstractions;

#if XUNIT_FRAMEWORK
Expand Down Expand Up @@ -33,6 +36,23 @@ public static string CombineStackTraces(IFailureInformation failureInfo)
return GetStackTrace(failureInfo, 0);
}

#if XUNIT_FRAMEWORK
static readonly ConcurrentDictionary<Type, PropertyInfo> innerExceptionsPropertyByType = new();

static IEnumerable<Exception> GetInnerExceptions(Exception ex)
{
if (ex is AggregateException aggEx)
return aggEx.InnerExceptions;

var prop = innerExceptionsPropertyByType.GetOrAdd(
ex.GetType(),
t => t.GetRuntimeProperties().FirstOrDefault(p => p.Name == "InnerExceptions" && p.CanRead)
);

return prop?.GetValue(ex) as IEnumerable<Exception>;
}
#endif

static bool ExcludeStackFrame(string stackFrame)
{
Guard.ArgumentNotNull("stackFrame", stackFrame);
Expand Down Expand Up @@ -176,9 +196,9 @@ static void ConvertExceptionToFailureInformation(Exception ex, int parentIndex,
indices.Add(parentIndex);

#if XUNIT_FRAMEWORK
var aggEx = ex as AggregateException;
if (aggEx != null)
foreach (var innerException in aggEx.InnerExceptions)
var innerExceptions = GetInnerExceptions(ex);
if (innerExceptions != null)
foreach (var innerException in innerExceptions)
ConvertExceptionToFailureInformation(innerException, myIndex, exceptionTypes, messages, stackTraces, indices);
else
#endif
Expand All @@ -193,6 +213,5 @@ class FailureInformation : IFailureInformation
public string[] StackTraces { get; set; }
public int[] ExceptionParentIndices { get; set; }
}

}
}

0 comments on commit 42307e8

Please sign in to comment.