Catch.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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
  26. {
  27. Disposable = e
  28. };
  29. var d = Disposable.Create(cts, a);
  30. var done = false;
  31. var f = default(Func<CancellationToken, Task<bool>>);
  32. f = async ct =>
  33. {
  34. if (!done)
  35. {
  36. try
  37. {
  38. return await e.MoveNext(ct)
  39. .ConfigureAwait(false);
  40. }
  41. catch (TException ex)
  42. {
  43. var err = handler(ex)
  44. .GetEnumerator();
  45. e = err;
  46. a.Disposable = e;
  47. done = true;
  48. return await f(ct)
  49. .ConfigureAwait(false);
  50. }
  51. }
  52. return await e.MoveNext(ct)
  53. .ConfigureAwait(false);
  54. };
  55. return Create(
  56. f,
  57. () => e.Current,
  58. d.Dispose,
  59. a
  60. );
  61. });
  62. }
  63. public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  64. {
  65. if (sources == null)
  66. throw new ArgumentNullException(nameof(sources));
  67. return sources.Catch_();
  68. }
  69. public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources)
  70. {
  71. if (sources == null)
  72. throw new ArgumentNullException(nameof(sources));
  73. return sources.Catch_();
  74. }
  75. public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  76. {
  77. if (first == null)
  78. throw new ArgumentNullException(nameof(first));
  79. if (second == null)
  80. throw new ArgumentNullException(nameof(second));
  81. return new[] { first, second }.Catch_();
  82. }
  83. private static IAsyncEnumerable<TSource> Catch_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  84. {
  85. return Create(() =>
  86. {
  87. var se = sources.GetEnumerator();
  88. var e = default(IAsyncEnumerator<TSource>);
  89. var cts = new CancellationTokenDisposable();
  90. var a = new AssignableDisposable();
  91. var d = Disposable.Create(cts, se, a);
  92. var error = default(ExceptionDispatchInfo);
  93. var f = default(Func<CancellationToken, Task<bool>>);
  94. f = async ct =>
  95. {
  96. if (e == null)
  97. {
  98. if (se.MoveNext())
  99. {
  100. e = se.Current.GetEnumerator();
  101. }
  102. else
  103. {
  104. error?.Throw();
  105. return false;
  106. }
  107. error = null;
  108. a.Disposable = e;
  109. }
  110. try
  111. {
  112. return await e.MoveNext(ct)
  113. .ConfigureAwait(false);
  114. }
  115. catch (Exception exception)
  116. {
  117. e.Dispose();
  118. e = null;
  119. error = ExceptionDispatchInfo.Capture(exception);
  120. return await f(ct)
  121. .ConfigureAwait(false);
  122. }
  123. };
  124. return Create(
  125. f,
  126. () => e.Current,
  127. d.Dispose,
  128. a
  129. );
  130. });
  131. }
  132. }
  133. }