Catch.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 async Task DisposeAsync()
  64. {
  65. if (enumerator != null)
  66. {
  67. await enumerator.DisposeAsync().ConfigureAwait(false);
  68. enumerator = null;
  69. }
  70. await base.DisposeAsync().ConfigureAwait(false);
  71. }
  72. protected override async Task<bool> MoveNextCore()
  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().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. .GetAsyncEnumerator();
  101. if (enumerator != null)
  102. {
  103. await enumerator.DisposeAsync().ConfigureAwait(false);
  104. }
  105. enumerator = err;
  106. isDone = true;
  107. continue; // loop so we hit the catch state
  108. }
  109. }
  110. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  111. {
  112. current = enumerator.Current;
  113. return true;
  114. }
  115. break; // while
  116. }
  117. break; // case
  118. }
  119. await DisposeAsync().ConfigureAwait(false);
  120. return false;
  121. }
  122. }
  123. private sealed class CatchAsyncIterator<TSource> : AsyncIterator<TSource>
  124. {
  125. private readonly IEnumerable<IAsyncEnumerable<TSource>> sources;
  126. private IAsyncEnumerator<TSource> enumerator;
  127. private ExceptionDispatchInfo error;
  128. private IEnumerator<IAsyncEnumerable<TSource>> sourcesEnumerator;
  129. public CatchAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  130. {
  131. Debug.Assert(sources != null);
  132. this.sources = sources;
  133. }
  134. public override AsyncIterator<TSource> Clone()
  135. {
  136. return new CatchAsyncIterator<TSource>(sources);
  137. }
  138. public override async Task DisposeAsync()
  139. {
  140. if (sourcesEnumerator != null)
  141. {
  142. sourcesEnumerator.Dispose();
  143. sourcesEnumerator = null;
  144. }
  145. if (enumerator != null)
  146. {
  147. await enumerator.DisposeAsync().ConfigureAwait(false);
  148. enumerator = null;
  149. }
  150. error = null;
  151. await base.DisposeAsync().ConfigureAwait(false);
  152. }
  153. protected override async Task<bool> MoveNextCore()
  154. {
  155. switch (state)
  156. {
  157. case AsyncIteratorState.Allocated:
  158. sourcesEnumerator = sources.GetEnumerator();
  159. state = AsyncIteratorState.Iterating;
  160. goto case AsyncIteratorState.Iterating;
  161. case AsyncIteratorState.Iterating:
  162. while (true)
  163. {
  164. if (enumerator == null)
  165. {
  166. if (!sourcesEnumerator.MoveNext())
  167. {
  168. // only throw if we have an error on the last one
  169. error?.Throw();
  170. break; // done, nothing else to do
  171. }
  172. error = null;
  173. enumerator = sourcesEnumerator.Current.GetAsyncEnumerator();
  174. }
  175. try
  176. {
  177. if (await enumerator.MoveNextAsync().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. await enumerator.DisposeAsync().ConfigureAwait(false);
  187. enumerator = null;
  188. error = ExceptionDispatchInfo.Capture(ex);
  189. continue;
  190. }
  191. break; // while
  192. }
  193. break; // case
  194. }
  195. await DisposeAsync().ConfigureAwait(false);
  196. return false;
  197. }
  198. }
  199. }
  200. }