AsyncEnumerable.Exceptions.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 Create(() =>
  22. {
  23. var e = source.GetEnumerator();
  24. var cts = new CancellationTokenDisposable();
  25. var a = new AssignableDisposable { Disposable = e };
  26. var d = Disposable.Create(cts, a);
  27. var done = false;
  28. var f = default(Func<CancellationToken, Task<bool>>);
  29. f = async ct =>
  30. {
  31. if (!done)
  32. {
  33. try
  34. {
  35. return await e.MoveNext(ct).ConfigureAwait(false);
  36. }
  37. catch (TException ex)
  38. {
  39. var err = handler(ex).GetEnumerator();
  40. e = err;
  41. a.Disposable = e;
  42. done = true;
  43. return await f(ct).ConfigureAwait(false);
  44. }
  45. }
  46. return await e.MoveNext(ct).ConfigureAwait(false);
  47. };
  48. return Create(
  49. f,
  50. () => e.Current,
  51. d.Dispose,
  52. a
  53. );
  54. });
  55. }
  56. public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  57. {
  58. if (sources == null)
  59. throw new ArgumentNullException(nameof(sources));
  60. return sources.Catch_();
  61. }
  62. public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources)
  63. {
  64. if (sources == null)
  65. throw new ArgumentNullException(nameof(sources));
  66. return sources.Catch_();
  67. }
  68. public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  69. {
  70. if (first == null)
  71. throw new ArgumentNullException(nameof(first));
  72. if (second == null)
  73. throw new ArgumentNullException(nameof(second));
  74. return new[] { first, second }.Catch_();
  75. }
  76. private static IAsyncEnumerable<TSource> Catch_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  77. {
  78. return Create(() =>
  79. {
  80. var se = sources.GetEnumerator();
  81. var e = default(IAsyncEnumerator<TSource>);
  82. var cts = new CancellationTokenDisposable();
  83. var a = new AssignableDisposable();
  84. var d = Disposable.Create(cts, se, a);
  85. var error = default(ExceptionDispatchInfo);
  86. var f = default(Func<CancellationToken, Task<bool>>);
  87. f = async ct =>
  88. {
  89. if (e == null)
  90. {
  91. if (se.MoveNext())
  92. {
  93. e = se.Current.GetEnumerator();
  94. }
  95. else
  96. {
  97. error?.Throw();
  98. return false;
  99. }
  100. error = null;
  101. a.Disposable = e;
  102. }
  103. try
  104. {
  105. return await e.MoveNext(ct).ConfigureAwait(false);
  106. }
  107. catch (Exception exception)
  108. {
  109. e.Dispose();
  110. e = null;
  111. error = ExceptionDispatchInfo.Capture(exception);
  112. return await f(ct).ConfigureAwait(false);
  113. }
  114. };
  115. return Create(
  116. f,
  117. () => e.Current,
  118. d.Dispose,
  119. a
  120. );
  121. });
  122. }
  123. public static IAsyncEnumerable<TSource> Finally<TSource>(this IAsyncEnumerable<TSource> source, Action finallyAction)
  124. {
  125. if (source == null)
  126. throw new ArgumentNullException(nameof(source));
  127. if (finallyAction == null)
  128. throw new ArgumentNullException(nameof(finallyAction));
  129. return Create(() =>
  130. {
  131. var e = source.GetEnumerator();
  132. var cts = new CancellationTokenDisposable();
  133. var r = new Disposable(finallyAction);
  134. var d = Disposable.Create(cts, e, r);
  135. return Create(
  136. ct => e.MoveNext(ct),
  137. () => e.Current,
  138. d.Dispose,
  139. r
  140. );
  141. });
  142. }
  143. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  144. {
  145. if (first == null)
  146. throw new ArgumentNullException(nameof(first));
  147. if (second == null)
  148. throw new ArgumentNullException(nameof(second));
  149. return OnErrorResumeNext_(new[] { first, second });
  150. }
  151. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources)
  152. {
  153. if (sources == null)
  154. throw new ArgumentNullException(nameof(sources));
  155. return OnErrorResumeNext_(sources);
  156. }
  157. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  158. {
  159. if (sources == null)
  160. throw new ArgumentNullException(nameof(sources));
  161. return OnErrorResumeNext_(sources);
  162. }
  163. private static IAsyncEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  164. {
  165. return Create(() =>
  166. {
  167. var se = sources.GetEnumerator();
  168. var e = default(IAsyncEnumerator<TSource>);
  169. var cts = new CancellationTokenDisposable();
  170. var a = new AssignableDisposable();
  171. var d = Disposable.Create(cts, se, a);
  172. var f = default(Func<CancellationToken, Task<bool>>);
  173. f = async ct =>
  174. {
  175. if (e == null)
  176. {
  177. var b = false;
  178. b = se.MoveNext();
  179. if (b)
  180. e = se.Current.GetEnumerator();
  181. else
  182. {
  183. return false;
  184. }
  185. a.Disposable = e;
  186. }
  187. try
  188. {
  189. if (await e.MoveNext(ct).ConfigureAwait(false))
  190. {
  191. return true;
  192. }
  193. }
  194. catch
  195. {
  196. // ignore
  197. }
  198. e.Dispose();
  199. e = null;
  200. return await f(ct).ConfigureAwait(false);
  201. };
  202. return Create(
  203. f,
  204. () => e.Current,
  205. d.Dispose,
  206. a
  207. );
  208. });
  209. }
  210. public static IAsyncEnumerable<TSource> Retry<TSource>(this IAsyncEnumerable<TSource> source)
  211. {
  212. if (source == null)
  213. throw new ArgumentNullException(nameof(source));
  214. return new[] { source }.Repeat().Catch();
  215. }
  216. public static IAsyncEnumerable<TSource> Retry<TSource>(this IAsyncEnumerable<TSource> source, int retryCount)
  217. {
  218. if (source == null)
  219. throw new ArgumentNullException(nameof(source));
  220. if (retryCount < 0)
  221. throw new ArgumentOutOfRangeException(nameof(retryCount));
  222. return new[] { source }.Repeat(retryCount).Catch();
  223. }
  224. private static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source)
  225. {
  226. while (true)
  227. foreach (var item in source)
  228. yield return item;
  229. }
  230. private static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source, int count)
  231. {
  232. for (var i = 0; i < count; i++)
  233. foreach (var item in source)
  234. yield return item;
  235. }
  236. }
  237. }