Catch.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Catch<TSource, TException>(this IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler)
  13. where TException : Exception
  14. {
  15. if (source == null)
  16. throw new ArgumentNullException(nameof(source));
  17. if (handler == null)
  18. throw new ArgumentNullException(nameof(handler));
  19. return new CatchAsyncIterator<TSource, TException>(source, handler);
  20. }
  21. public static IAsyncEnumerable<TSource> Catch<TSource, TException>(this IAsyncEnumerable<TSource> source, Func<TException, Task<IAsyncEnumerable<TSource>>> handler)
  22. where TException : Exception
  23. {
  24. if (source == null)
  25. throw new ArgumentNullException(nameof(source));
  26. if (handler == null)
  27. throw new ArgumentNullException(nameof(handler));
  28. return new CatchAsyncIteratorWithTask<TSource, TException>(source, handler);
  29. }
  30. public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  31. {
  32. if (sources == null)
  33. throw new ArgumentNullException(nameof(sources));
  34. return CatchCore(sources);
  35. }
  36. public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources)
  37. {
  38. if (sources == null)
  39. throw new ArgumentNullException(nameof(sources));
  40. return CatchCore(sources);
  41. }
  42. public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  43. {
  44. if (first == null)
  45. throw new ArgumentNullException(nameof(first));
  46. if (second == null)
  47. throw new ArgumentNullException(nameof(second));
  48. return CatchCore(new[] { first, second });
  49. }
  50. private static IAsyncEnumerable<TSource> CatchCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  51. {
  52. return new CatchAsyncIterator<TSource>(sources);
  53. }
  54. private sealed class CatchAsyncIterator<TSource, TException> : AsyncIterator<TSource> where TException : Exception
  55. {
  56. private readonly Func<TException, IAsyncEnumerable<TSource>> handler;
  57. private readonly IAsyncEnumerable<TSource> source;
  58. private IAsyncEnumerator<TSource> enumerator;
  59. private bool isDone;
  60. public CatchAsyncIterator(IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler)
  61. {
  62. Debug.Assert(source != null);
  63. Debug.Assert(handler != null);
  64. this.source = source;
  65. this.handler = handler;
  66. }
  67. public override AsyncIterator<TSource> Clone()
  68. {
  69. return new CatchAsyncIterator<TSource, TException>(source, handler);
  70. }
  71. public override async Task DisposeAsync()
  72. {
  73. if (enumerator != null)
  74. {
  75. await enumerator.DisposeAsync().ConfigureAwait(false);
  76. enumerator = null;
  77. }
  78. await base.DisposeAsync().ConfigureAwait(false);
  79. }
  80. protected override async Task<bool> MoveNextCore()
  81. {
  82. switch (state)
  83. {
  84. case AsyncIteratorState.Allocated:
  85. enumerator = source.GetAsyncEnumerator();
  86. isDone = false;
  87. state = AsyncIteratorState.Iterating;
  88. goto case AsyncIteratorState.Iterating;
  89. case AsyncIteratorState.Iterating:
  90. while (true)
  91. {
  92. if (!isDone)
  93. {
  94. try
  95. {
  96. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  97. {
  98. current = enumerator.Current;
  99. return true;
  100. }
  101. }
  102. catch (TException ex)
  103. {
  104. // Note: Ideally we'd dipose of the previous enumerator before
  105. // invoking the handler, but we use this order to preserve
  106. // current behavior
  107. var inner = handler(ex);
  108. var err = inner.GetAsyncEnumerator();
  109. if (enumerator != null)
  110. {
  111. await enumerator.DisposeAsync().ConfigureAwait(false);
  112. }
  113. enumerator = err;
  114. isDone = true;
  115. continue; // loop so we hit the catch state
  116. }
  117. }
  118. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  119. {
  120. current = enumerator.Current;
  121. return true;
  122. }
  123. break; // while
  124. }
  125. break; // case
  126. }
  127. await DisposeAsync().ConfigureAwait(false);
  128. return false;
  129. }
  130. }
  131. private sealed class CatchAsyncIteratorWithTask<TSource, TException> : AsyncIterator<TSource> where TException : Exception
  132. {
  133. private readonly Func<TException, Task<IAsyncEnumerable<TSource>>> handler;
  134. private readonly IAsyncEnumerable<TSource> source;
  135. private IAsyncEnumerator<TSource> enumerator;
  136. private bool isDone;
  137. public CatchAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<TException, Task<IAsyncEnumerable<TSource>>> handler)
  138. {
  139. Debug.Assert(source != null);
  140. Debug.Assert(handler != null);
  141. this.source = source;
  142. this.handler = handler;
  143. }
  144. public override AsyncIterator<TSource> Clone()
  145. {
  146. return new CatchAsyncIteratorWithTask<TSource, TException>(source, handler);
  147. }
  148. public override async Task DisposeAsync()
  149. {
  150. if (enumerator != null)
  151. {
  152. await enumerator.DisposeAsync().ConfigureAwait(false);
  153. enumerator = null;
  154. }
  155. await base.DisposeAsync().ConfigureAwait(false);
  156. }
  157. protected override async Task<bool> MoveNextCore()
  158. {
  159. switch (state)
  160. {
  161. case AsyncIteratorState.Allocated:
  162. enumerator = source.GetAsyncEnumerator();
  163. isDone = false;
  164. state = AsyncIteratorState.Iterating;
  165. goto case AsyncIteratorState.Iterating;
  166. case AsyncIteratorState.Iterating:
  167. while (true)
  168. {
  169. if (!isDone)
  170. {
  171. try
  172. {
  173. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  174. {
  175. current = enumerator.Current;
  176. return true;
  177. }
  178. }
  179. catch (TException ex)
  180. {
  181. // Note: Ideally we'd dipose of the previous enumerator before
  182. // invoking the handler, but we use this order to preserve
  183. // current behavior
  184. var inner = await handler(ex).ConfigureAwait(false);
  185. var err = inner.GetAsyncEnumerator();
  186. if (enumerator != null)
  187. {
  188. await enumerator.DisposeAsync().ConfigureAwait(false);
  189. }
  190. enumerator = err;
  191. isDone = true;
  192. continue; // loop so we hit the catch state
  193. }
  194. }
  195. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  196. {
  197. current = enumerator.Current;
  198. return true;
  199. }
  200. break; // while
  201. }
  202. break; // case
  203. }
  204. await DisposeAsync().ConfigureAwait(false);
  205. return false;
  206. }
  207. }
  208. private sealed class CatchAsyncIterator<TSource> : AsyncIterator<TSource>
  209. {
  210. private readonly IEnumerable<IAsyncEnumerable<TSource>> sources;
  211. private IAsyncEnumerator<TSource> enumerator;
  212. private ExceptionDispatchInfo error;
  213. private IEnumerator<IAsyncEnumerable<TSource>> sourcesEnumerator;
  214. public CatchAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  215. {
  216. Debug.Assert(sources != null);
  217. this.sources = sources;
  218. }
  219. public override AsyncIterator<TSource> Clone()
  220. {
  221. return new CatchAsyncIterator<TSource>(sources);
  222. }
  223. public override async Task DisposeAsync()
  224. {
  225. if (sourcesEnumerator != null)
  226. {
  227. sourcesEnumerator.Dispose();
  228. sourcesEnumerator = null;
  229. }
  230. if (enumerator != null)
  231. {
  232. await enumerator.DisposeAsync().ConfigureAwait(false);
  233. enumerator = null;
  234. }
  235. error = null;
  236. await base.DisposeAsync().ConfigureAwait(false);
  237. }
  238. protected override async Task<bool> MoveNextCore()
  239. {
  240. switch (state)
  241. {
  242. case AsyncIteratorState.Allocated:
  243. sourcesEnumerator = sources.GetEnumerator();
  244. state = AsyncIteratorState.Iterating;
  245. goto case AsyncIteratorState.Iterating;
  246. case AsyncIteratorState.Iterating:
  247. while (true)
  248. {
  249. if (enumerator == null)
  250. {
  251. if (!sourcesEnumerator.MoveNext())
  252. {
  253. // only throw if we have an error on the last one
  254. error?.Throw();
  255. break; // done, nothing else to do
  256. }
  257. error = null;
  258. enumerator = sourcesEnumerator.Current.GetAsyncEnumerator();
  259. }
  260. try
  261. {
  262. if (await enumerator.MoveNextAsync().ConfigureAwait(false))
  263. {
  264. current = enumerator.Current;
  265. return true;
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. // Done with the current one, go to the next
  271. await enumerator.DisposeAsync().ConfigureAwait(false);
  272. enumerator = null;
  273. error = ExceptionDispatchInfo.Capture(ex);
  274. continue;
  275. }
  276. break; // while
  277. }
  278. break; // case
  279. }
  280. await DisposeAsync().ConfigureAwait(false);
  281. return false;
  282. }
  283. }
  284. }
  285. }