Catch.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.ExceptionServices;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Catch<TSource, TException>(this IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler)
  14. where TException : Exception
  15. {
  16. if (source == null)
  17. throw new ArgumentNullException(nameof(source));
  18. if (handler == null)
  19. throw new ArgumentNullException(nameof(handler));
  20. return new CatchAsyncIterator<TSource, TException>(source, handler);
  21. }
  22. public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  23. {
  24. if (sources == null)
  25. throw new ArgumentNullException(nameof(sources));
  26. return sources.Catch_();
  27. }
  28. public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources)
  29. {
  30. if (sources == null)
  31. throw new ArgumentNullException(nameof(sources));
  32. return sources.Catch_();
  33. }
  34. public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  35. {
  36. if (first == null)
  37. throw new ArgumentNullException(nameof(first));
  38. if (second == null)
  39. throw new ArgumentNullException(nameof(second));
  40. return new[] {first, second}.Catch_();
  41. }
  42. private static IAsyncEnumerable<TSource> Catch_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  43. {
  44. return new CatchAsyncIterator<TSource>(sources);
  45. }
  46. private sealed class CatchAsyncIterator<TSource, TException> : AsyncIterator<TSource> where TException : Exception
  47. {
  48. private readonly Func<TException, IAsyncEnumerable<TSource>> handler;
  49. private readonly IAsyncEnumerable<TSource> source;
  50. private IAsyncEnumerator<TSource> enumerator;
  51. private bool isDone;
  52. public CatchAsyncIterator(IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler)
  53. {
  54. Debug.Assert(source != null);
  55. Debug.Assert(handler != null);
  56. this.source = source;
  57. this.handler = handler;
  58. }
  59. public override AsyncIterator<TSource> Clone()
  60. {
  61. return new CatchAsyncIterator<TSource, TException>(source, handler);
  62. }
  63. public override void Dispose()
  64. {
  65. if (enumerator != null)
  66. {
  67. enumerator.Dispose();
  68. enumerator = null;
  69. }
  70. base.Dispose();
  71. }
  72. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  73. {
  74. switch (state)
  75. {
  76. case AsyncIteratorState.Allocated:
  77. enumerator = source.GetAsyncEnumerator();
  78. isDone = false;
  79. state = AsyncIteratorState.Iterating;
  80. goto case AsyncIteratorState.Iterating;
  81. case AsyncIteratorState.Iterating:
  82. while (true)
  83. {
  84. if (!isDone)
  85. {
  86. try
  87. {
  88. if (await enumerator.MoveNextAsync(cancellationToken)
  89. .ConfigureAwait(false))
  90. {
  91. current = enumerator.Current;
  92. return true;
  93. }
  94. }
  95. catch (TException ex)
  96. {
  97. // Note: Ideally we'd dipose of the previous enumerator before
  98. // invoking the handler, but we use this order to preserve
  99. // current behavior
  100. var err = handler(ex)
  101. .GetAsyncEnumerator();
  102. enumerator?.Dispose();
  103. enumerator = err;
  104. isDone = true;
  105. continue; // loop so we hit the catch state
  106. }
  107. }
  108. if (await enumerator.MoveNextAsync(cancellationToken)
  109. .ConfigureAwait(false))
  110. {
  111. current = enumerator.Current;
  112. return true;
  113. }
  114. break; // while
  115. }
  116. break; // case
  117. }
  118. Dispose();
  119. return false;
  120. }
  121. }
  122. private sealed class CatchAsyncIterator<TSource> : AsyncIterator<TSource>
  123. {
  124. private readonly IEnumerable<IAsyncEnumerable<TSource>> sources;
  125. private IAsyncEnumerator<TSource> enumerator;
  126. private ExceptionDispatchInfo error;
  127. private IEnumerator<IAsyncEnumerable<TSource>> sourcesEnumerator;
  128. public CatchAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  129. {
  130. Debug.Assert(sources != null);
  131. this.sources = sources;
  132. }
  133. public override AsyncIterator<TSource> Clone()
  134. {
  135. return new CatchAsyncIterator<TSource>(sources);
  136. }
  137. public override void Dispose()
  138. {
  139. if (sourcesEnumerator != null)
  140. {
  141. sourcesEnumerator.Dispose();
  142. sourcesEnumerator = null;
  143. }
  144. if (enumerator != null)
  145. {
  146. enumerator.Dispose();
  147. enumerator = null;
  148. }
  149. error = null;
  150. base.Dispose();
  151. }
  152. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  153. {
  154. switch (state)
  155. {
  156. case AsyncIteratorState.Allocated:
  157. sourcesEnumerator = sources.GetEnumerator();
  158. state = AsyncIteratorState.Iterating;
  159. goto case AsyncIteratorState.Iterating;
  160. case AsyncIteratorState.Iterating:
  161. while (true)
  162. {
  163. if (enumerator == null)
  164. {
  165. if (!sourcesEnumerator.MoveNext())
  166. {
  167. // only throw if we have an error on the last one
  168. error?.Throw();
  169. break; // done, nothing else to do
  170. }
  171. error = null;
  172. enumerator = sourcesEnumerator.Current.GetAsyncEnumerator();
  173. }
  174. try
  175. {
  176. if (await enumerator.MoveNextAsync(cancellationToken)
  177. .ConfigureAwait(false))
  178. {
  179. current = enumerator.Current;
  180. return true;
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. // Done with the current one, go to the next
  186. enumerator.Dispose();
  187. enumerator = null;
  188. error = ExceptionDispatchInfo.Capture(ex);
  189. continue;
  190. }
  191. break; // while
  192. }
  193. break; // case
  194. }
  195. Dispose();
  196. return false;
  197. }
  198. }
  199. }
  200. }