Catch.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Runtime.ExceptionServices;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace System.Linq
  11. {
  12. public static partial class AsyncEnumerable
  13. {
  14. public static IAsyncEnumerable<TSource> Catch<TSource, TException>(this IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler)
  15. where TException : Exception
  16. {
  17. if (source == null)
  18. throw new ArgumentNullException(nameof(source));
  19. if (handler == null)
  20. throw new ArgumentNullException(nameof(handler));
  21. return CreateEnumerable(
  22. () =>
  23. {
  24. var e = source.GetEnumerator();
  25. var cts = new CancellationTokenDisposable();
  26. var a = new AssignableDisposable
  27. {
  28. Disposable = e
  29. };
  30. var d = Disposable.Create(cts, a);
  31. var done = false;
  32. var f = default(Func<CancellationToken, Task<bool>>);
  33. f = async ct =>
  34. {
  35. if (!done)
  36. {
  37. try
  38. {
  39. return await e.MoveNext(ct)
  40. .ConfigureAwait(false);
  41. }
  42. catch (TException ex)
  43. {
  44. var err = handler(ex)
  45. .GetEnumerator();
  46. e = err;
  47. a.Disposable = e;
  48. done = true;
  49. return await f(ct)
  50. .ConfigureAwait(false);
  51. }
  52. }
  53. return await e.MoveNext(ct)
  54. .ConfigureAwait(false);
  55. };
  56. return CreateEnumerator(
  57. f,
  58. () => e.Current,
  59. d.Dispose,
  60. a
  61. );
  62. });
  63. }
  64. public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  65. {
  66. if (sources == null)
  67. throw new ArgumentNullException(nameof(sources));
  68. return sources.Catch_();
  69. }
  70. public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources)
  71. {
  72. if (sources == null)
  73. throw new ArgumentNullException(nameof(sources));
  74. return sources.Catch_();
  75. }
  76. public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  77. {
  78. if (first == null)
  79. throw new ArgumentNullException(nameof(first));
  80. if (second == null)
  81. throw new ArgumentNullException(nameof(second));
  82. return new[] {first, second}.Catch_();
  83. }
  84. private static IAsyncEnumerable<TSource> Catch_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  85. {
  86. return CreateEnumerable(
  87. () =>
  88. {
  89. var se = sources.GetEnumerator();
  90. var e = default(IAsyncEnumerator<TSource>);
  91. var cts = new CancellationTokenDisposable();
  92. var a = new AssignableDisposable();
  93. var d = Disposable.Create(cts, se, a);
  94. var error = default(ExceptionDispatchInfo);
  95. var f = default(Func<CancellationToken, Task<bool>>);
  96. f = async ct =>
  97. {
  98. if (e == null)
  99. {
  100. if (se.MoveNext())
  101. {
  102. e = se.Current.GetEnumerator();
  103. }
  104. else
  105. {
  106. error?.Throw();
  107. return false;
  108. }
  109. error = null;
  110. a.Disposable = e;
  111. }
  112. try
  113. {
  114. return await e.MoveNext(ct)
  115. .ConfigureAwait(false);
  116. }
  117. catch (Exception exception)
  118. {
  119. e.Dispose();
  120. e = null;
  121. error = ExceptionDispatchInfo.Capture(exception);
  122. return await f(ct)
  123. .ConfigureAwait(false);
  124. }
  125. };
  126. return CreateEnumerator(
  127. f,
  128. () => e.Current,
  129. d.Dispose,
  130. a
  131. );
  132. });
  133. }
  134. }
  135. }