Skip to content

Commit

Permalink
Add extension method "GetInnerMostExceptions()"
Browse files Browse the repository at this point in the history
  • Loading branch information
skrysmanski committed Mar 24, 2024
1 parent be47fa9 commit 85d0085
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/AppMotor.Core/Extensions/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public static Exception Rethrow(this Exception exception)
/// <see cref="AggregateException"/>s no matter how many exceptions actually occurred.</para>
/// </summary>
/// <param name="aggregateException">The exception to unroll</param>
/// <param name="deepUnroll">If <c>true</c> and the only inner exception is also a
/// <param name="deepUnroll">If <c>true</c> and the only inner exception is also an
/// <seealso cref="AggregateException"/>, this inner exception will also be unrolled (and so forth).
/// If <c>false</c>, only this exception will be unrolled. In case of doubt, use <c>false</c> and
/// change it to <c>true</c> if see the need.</param>
/// change it to <c>true</c> if you see the need.</param>
/// <param name="preventUnrollingOnExistingExceptionData">When unrolling, should existing exception
/// data (<see cref="Exception.Data"/>) prevent unrolling (<c>true</c>; the default). The reasoning
/// here is that if the <see cref="AggregateException"/> itself contains exception data, it would
Expand Down Expand Up @@ -150,4 +150,45 @@ bool preventUnrollingOnExistingExceptionData
return onlyInnerException;
}
}

/// <summary>
/// Returns the innermost exception(s), i.e. the exception(s) that don't have any inner exceptions.
/// Note that because <see cref="AggregateException"/>s can have more than one inner exception, this
/// method may return more than one innermost exception.
/// </summary>
[PublicAPI, MustUseReturnValue]
public static IEnumerable<Exception> GetInnerMostExceptions(this Exception exception)
{
if (exception is AggregateException aggregateException)
{
if (aggregateException.InnerExceptions.Count == 0)
{
yield return aggregateException;
}
else
{
foreach (var innerException in aggregateException.InnerExceptions)
{
foreach (var innerMostExceptions in innerException.GetInnerMostExceptions())
{
yield return innerMostExceptions;
}
}
}
}
else
{
if (exception.InnerException is null)
{
yield return exception;
}
else
{
foreach (var innerMostException in exception.InnerException.GetInnerMostExceptions())
{
yield return innerMostException;
}
}
}
}
}

0 comments on commit 85d0085

Please sign in to comment.