Catch.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. if (!isDone)
  82. {
  83. try
  84. {
  85. if (await enumerator.MoveNext(cancellationToken)
  86. .ConfigureAwait(false))
  87. {
  88. current = enumerator.Current;
  89. return true;
  90. }
  91. }
  92. catch (TException ex)
  93. {
  94. var err = handler(ex)
  95. .GetEnumerator();
  96. enumerator?.Dispose();
  97. enumerator = err;
  98. isDone = true;
  99. goto case AsyncIteratorState.Iterating; // loop so we hit the catch state
  100. }
  101. }
  102. if (await enumerator.MoveNext(cancellationToken)
  103. .ConfigureAwait(false))
  104. {
  105. current = enumerator.Current;
  106. return true;
  107. }
  108. break;
  109. }
  110. Dispose();
  111. return false;
  112. }
  113. }
  114. private sealed class CatchAsyncIterator<TSource> : AsyncIterator<TSource>
  115. {
  116. private readonly IEnumerable<IAsyncEnumerable<TSource>> sources;
  117. private IAsyncEnumerator<TSource> enumerator;
  118. private ExceptionDispatchInfo error;
  119. private IEnumerator<IAsyncEnumerable<TSource>> sourcesEnumerator;
  120. public CatchAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  121. {
  122. this.sources = sources;
  123. }
  124. public override AsyncIterator<TSource> Clone()
  125. {
  126. return new CatchAsyncIterator<TSource>(sources);
  127. }
  128. public override void Dispose()
  129. {
  130. if (sourcesEnumerator != null)
  131. {
  132. sourcesEnumerator.Dispose();
  133. sourcesEnumerator = null;
  134. }
  135. if (enumerator != null)
  136. {
  137. enumerator.Dispose();
  138. enumerator = null;
  139. }
  140. error = null;
  141. base.Dispose();
  142. }
  143. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  144. {
  145. switch (state)
  146. {
  147. case AsyncIteratorState.Allocated:
  148. sourcesEnumerator = sources.GetEnumerator();
  149. state = AsyncIteratorState.Iterating;
  150. goto case AsyncIteratorState.Iterating;
  151. case AsyncIteratorState.Iterating:
  152. if (enumerator == null)
  153. {
  154. if (!sourcesEnumerator.MoveNext())
  155. {
  156. // only throw if we have an error on the last one
  157. error?.Throw();
  158. break; // done, nothing else to do
  159. }
  160. error = null;
  161. enumerator = sourcesEnumerator.Current.GetEnumerator();
  162. }
  163. try
  164. {
  165. if (await enumerator.MoveNext(cancellationToken)
  166. .ConfigureAwait(false))
  167. {
  168. current = enumerator.Current;
  169. return true;
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. // Done with the current one, go to the next
  175. enumerator.Dispose();
  176. enumerator = null;
  177. error = ExceptionDispatchInfo.Capture(ex);
  178. goto case AsyncIteratorState.Iterating;
  179. }
  180. break;
  181. }
  182. Dispose();
  183. return false;
  184. }
  185. }
  186. }
  187. }