Catch.cs 8.9 KB

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