Catch.cs 8.6 KB

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