Catch.cs 9.1 KB

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