{"record":{"id":"b53385e72f45666e","repo":"louthy/language-ext","slug":"aggregateexception-from-collected-inner-errors","errorCode":null,"errorMessage":"AggregateException from collected inner errors","messagePattern":"AggregateException from collected inner errors","errorType":"exception","errorClass":"AggregateException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Utility/AsyncEnumerableEx.cs","lineNumber":110,"sourceCode":"                            {\n                                await enumerator.DisposeAsync().ConfigureAwait(false);\n                            }\n                        }\n                    }\n                    catch (Exception ex)\n                    {\n                        if (errors == null)\n                        {\n                            errors = new List<Exception>();\n                        }\n\n                        errors.Add(ex);\n                    }\n                }\n\n                if (errors != null)\n                {\n                    throw new AggregateException(errors);\n                }\n            }\n        }\n\n    }\n\n    /// <summary>\n    /// Merges elements from all inner async-enumerable sequences into a single async-enumerable sequence.\n    /// </summary>\n    /// <typeparam name=\"TSource\">The type of the elements in the source sequences.</typeparam>\n    /// <param name=\"sources\">Async-enumerable sequence of inner async-enumerable sequences.</param>\n    /// <returns>The async-enumerable sequence that merges the elements of the inner sequences.</returns>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"sources\"/> is null.</exception>\n    public static IAsyncEnumerable<TSource> Merge<TSource>(\n        this IAsyncEnumerable<IAsyncEnumerable<TSource>> sources) =>\n        sources.SelectMany(source => source);\n}\n","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Utility/AsyncEnumerableEx.cs#L92-L128","documentation":"AsyncEnumerableEx.Merge aggregates the exceptions thrown by the merged source sequences; when any inner stream errors, the merge enumerates errors and throws a single AggregateException containing all collected inner errors at line 110. This is the library's way of reporting multiple concurrent stream failures from one Merge call. The real causes are the inner exceptions — inspect AggregateException.InnerExceptions.","triggerScenarios":"Calling Merge (or Merge with a concurrency limit) where one or more of the merged IAsyncEnumerable sources throws while the merged sequence is being consumed; all inner errors are gathered and rethrown together as AggregateException.","commonSituations":"Merging several HTTP/stream/event sources where some fail mid-enumeration (network faults, upstream 5xx, canceled subscriptions); a partially failing fan-in pipeline; consuming Merge output without handling AggregateException.","solutions":["Inspect ex.InnerExceptions on the AggregateException to handle each underlying failure","Wrap the Merge consumption in try/catch (AggregateException) and decide per-inner-error whether to retry or drop","Retry individual failing sources with backoff before merging, so Merge itself stays error-free","Use error-tolerant sources (catch inside each source stream and yield a sentinel) so one failure doesn't fail the merge"],"exampleFix":"// before\nawait foreach (var x in AsyncEnumerableEx.Merge(srcA, srcB))\n    Process(x); // throws AggregateException if either source fails\n// after\ntry\n{\n    await foreach (var x in AsyncEnumerableEx.Merge(srcA, srcB))\n        Process(x);\n}\ncatch (AggregateException agg)\n{\n    foreach (var inner in agg.InnerExceptions)\n        Log(inner); // handle/retry each failing source individually\n}","handlingStrategy":"try-catch","validationCode":"// pre-flight each source with a guard\nvar safeA = srcA.Catch(ex => AsyncEnumerable.Empty<T>());\nvar safeB = srcB.Catch(ex => AsyncEnumerable.Empty<T>());\nvar merged = AsyncEnumerableEx.Merge(safeA, safeB);","typeGuard":"static bool HasInnerErrors(AggregateException agg) => agg.InnerExceptions.Count > 0;","tryCatchPattern":"try { await foreach (var x in merged) Process(x); }\ncatch (AggregateException agg)\n{\n    foreach (var inner in agg.InnerExceptions)\n        HandleSourceError(inner); // retry/log per failing source\n}","preventionTips":["Wrap each merged source with .Catch so one failure doesn't kill the merge","Always catch AggregateException (not Exception) around Merge consumption","Add retry-with-backoff to flaky inner sources before merging","Log InnerExceptions individually to identify which source failed"],"tags":["csharp","languageext","async","async-enumerable","merge"],"backgroundTag":"aggregate-exception-from-merged-sequences","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}