|
|
@@ -1,4 +1,5 @@
|
|
|
using System;
|
|
|
+using System.Collections.Generic;
|
|
|
|
|
|
namespace Quadarax.Foundation.Core.Exceptions
|
|
|
{
|
|
|
@@ -35,6 +36,61 @@ namespace Quadarax.Foundation.Core.Exceptions
|
|
|
sb.AppendLine(e.StackTrace);
|
|
|
}
|
|
|
return sb.ToString();
|
|
|
- }
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Converts an Exception and its inner exceptions to an AggregateException.
|
|
|
+ /// Flattens the exception hierarchy into a single AggregateException containing all exceptions.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="exception">The exception to convert</param>
|
|
|
+ /// <returns>An AggregateException containing the original exception and all inner exceptions</returns>
|
|
|
+ public static AggregateException ToAggregateException(this Exception exception)
|
|
|
+ {
|
|
|
+ if (exception == null)
|
|
|
+ throw new ArgumentNullException(nameof(exception));
|
|
|
+
|
|
|
+ // If it's already an AggregateException, flatten it to avoid nested AggregateExceptions
|
|
|
+ if (exception is AggregateException aggregateException)
|
|
|
+ {
|
|
|
+ return aggregateException.Flatten();
|
|
|
+ }
|
|
|
+
|
|
|
+ var exceptions = new List<Exception>();
|
|
|
+ FlattenExceptions(exception, exceptions);
|
|
|
+
|
|
|
+ return new AggregateException(exceptions);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Recursively flattens exception hierarchy into a list
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="exception">Current exception to process</param>
|
|
|
+ /// <param name="exceptions">List to accumulate flattened exceptions</param>
|
|
|
+ private static void FlattenExceptions(Exception exception, List<Exception> exceptions)
|
|
|
+ {
|
|
|
+ if (exception == null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // Handle AggregateException specially to avoid deep nesting
|
|
|
+ if (exception is AggregateException aggEx)
|
|
|
+ {
|
|
|
+ foreach (var innerEx in aggEx.InnerExceptions)
|
|
|
+ {
|
|
|
+ FlattenExceptions(innerEx, exceptions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // Add the current exception
|
|
|
+ exceptions.Add(exception);
|
|
|
+
|
|
|
+ // Recursively process inner exception
|
|
|
+ if (exception.InnerException != null)
|
|
|
+ {
|
|
|
+ FlattenExceptions(exception.InnerException, exceptions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|