flatten method
Flattens all inner AggregateException instances into a single AggregateException.
This recursively flattens any nested AggregateException instances and returns a new AggregateException containing all leaf exceptions.
Implementation
AggregateException flatten() {
var flattenedExceptions = <Exception>[];
var exceptionsToFlatten = <Exception>[..._innerExceptions];
while (exceptionsToFlatten.isNotEmpty) {
var exception = exceptionsToFlatten.removeAt(0);
if (exception is AggregateException) {
// Add all inner exceptions from nested AggregateException
exceptionsToFlatten.insertAll(0, exception._innerExceptions);
} else {
// Add leaf exception
flattenedExceptions.add(exception);
}
}
return AggregateException(
message: _message,
innerExceptions: flattenedExceptions,
);
}