Debian Codegraph @

external/rx/Ix/NET/System.Interactive/EnumerableEx.Exceptions.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5
6 namespace System.Linq
7 {
8 public static partial class EnumerableEx
9 {
10 /// <summary>
11 /// Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from calling an exception handler function in case of an error.
12 /// </summary>
13 /// <typeparam name="TSource">Source sequence element type.</typeparam>
14 /// <typeparam name="TException">Exception type to catch.</typeparam>
15 /// <param name="source">Source sequence.</param>
16 /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
17 /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
18 public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
19 where TException : Exception
20 {
21 if (source == null)
22 throw new ArgumentNullException("source");
23 if (handler == null)
24 throw new ArgumentNullException("handler");
25
26 return source.Catch_(handler);
27 }
28
29 private static IEnumerable<TSource> Catch_<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
30 where TException : Exception
31 {
32 var err = default(IEnumerable<TSource>);
33
34 using (var e = source.GetEnumerator())
35 {
36 while (true)
37 {
38 var b = default(bool);
39 var c = default(TSource);
40
41 try
42 {
43 b = e.MoveNext();
44 c = e.Current;
45 }
46 catch (TException ex)
47 {
48 err = handler(ex);
49 break;
50 }
51
52 if (!b)
53 break;
54
55 yield return c;
56 }
57 }
58
59 if (err != null)
60 {
61 foreach (var item in err)
62 yield return item;
63 }
64 }
65
66 /// <summary>
67 /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
68 /// </summary>
69 /// <typeparam name="TSource">Source sequence element type.</typeparam>
70 /// <param name="sources">Source sequences.</param>
71 /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
72 public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
73 {
74 if (sources == null)
75 throw new ArgumentNullException("sources");
76
77 return sources.Catch_();
78 }
79
80 /// <summary>
81 /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
82 /// </summary>
83 /// <typeparam name="TSource">Source sequence element type.</typeparam>
84 /// <param name="sources">Source sequences.</param>
85 /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
86 public static IEnumerable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
87 {
88 if (sources == null)
89 throw new ArgumentNullException("sources");
90
91 return sources.Catch_();
92 }
93
94 /// <summary>
95 /// Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
96 /// </summary>
97 /// <typeparam name="TSource">Source sequence element type.</typeparam>
98 /// <param name="first">First sequence.</param>
99 /// <param name="second">Second sequence, concatenated to the result in case the first sequence completes exceptionally.</param>
100 /// <returns>The first sequence, followed by the second sequence in case an error is produced.</returns>
101 public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
102 {
103 if (first == null)
104 throw new ArgumentNullException("first");
105 if (second == null)
106 throw new ArgumentNullException("second");
107
108 return new[] { first, second }.Catch_();
109 }
110
111 private static IEnumerable<TSource> Catch_<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
112 {
113 var error = default(Exception);
114
115 foreach (var source in sources)
116 {
117 using (var e = source.GetEnumerator())
118 {
119 error = null;
120
121 while (true)
122 {
123 var b = default(bool);
124 var c = default(TSource);
125
126 try
127 {
128 b = e.MoveNext();
129 c = e.Current;
130 }
131 catch (Exception ex)
132 {
133 error = ex;
134 break;
135 }
136
137 if (!b)
138 break;
139
140 yield return c;
141 }
142
143 if (error == null)
144 break;
145 }
146 }
147
148 if (error != null)
149 throw error;
150 }
151
152 /// <summary>
153 /// Creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed.
154 /// </summary>
155 /// <typeparam name="TSource">Source sequence element type.</typeparam>
156 /// <param name="source">Source sequence.</param>
157 /// <param name="finallyAction">Action to run upon termination of the sequence, or when an enumerator is disposed.</param>
158 /// <returns>Source sequence with guarantees on the invocation of the finally action.</returns>
159 public static IEnumerable<TSource> Finally<TSource>(this IEnumerable<TSource> source, Action finallyAction)
160 {
161 if (source == null)
162 throw new ArgumentNullException("source");
163 if (finallyAction == null)
164 throw new ArgumentNullException("finallyAction");
165
166 return source.Finally_(finallyAction);
167 }
168
169 private static IEnumerable<TSource> Finally_<TSource>(this IEnumerable<TSource> source, Action finallyAction)
170 {
171 try
172 {
173 foreach (var item in source)
174 yield return item;
175 }
176 finally
177 {
178 finallyAction();
179 }
180 }
181
182 /// <summary>
183 /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
184 /// </summary>
185 /// <typeparam name="TSource">Source sequence element type.</typeparam>
186 /// <param name="first">First sequence.</param>
187 /// <param name="second">Second sequence.</param>
188 /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
189 public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
190 {
191 if (first == null)
192 throw new ArgumentNullException("first");
193 if (second == null)
194 throw new ArgumentNullException("second");
195
196 return OnErrorResumeNext_(new[] { first, second });
197 }
198
199 /// <summary>
200 /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
201 /// </summary>
202 /// <typeparam name="TSource">Source sequence element type.</typeparam>
203 /// <param name="sources">Source sequences.</param>
204 /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
205 public static IEnumerable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
206 {
207 if (sources == null)
208 throw new ArgumentNullException("sources");
209
210 return OnErrorResumeNext_(sources);
211 }
212
213 /// <summary>
214 /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
215 /// </summary>
216 /// <typeparam name="TSource">Source sequence element type.</typeparam>
217 /// <param name="sources">Source sequences.</param>
218 /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
219 public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
220 {
221 if (sources == null)
222 throw new ArgumentNullException("sources");
223
224 return OnErrorResumeNext_(sources);
225 }
226
227 private static IEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IEnumerable<TSource>> sources)
228 {
229 foreach (var source in sources)
230 {
231 using (var innerEnumerator = source.GetEnumerator())
232 {
233 while (true)
234 {
235 var value = default(TSource);
236 try
237 {
238 if (!innerEnumerator.MoveNext())
239 break;
240 value = innerEnumerator.Current;
241 }
242 catch
243 {
244 break;
245 }
246
247 yield return value;
248 }
249 }
250 }
251 }
252
253 /// <summary>
254 /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
255 /// </summary>
256 /// <typeparam name="TSource">Source sequence element type.</typeparam>
257 /// <param name="source">Source sequence.</param>
258 /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
259 public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source)
260 {
261 if (source == null)
262 throw new ArgumentNullException("source");
263
264 return new[] { source }.Repeat().Catch();
265 }
266
267 /// <summary>
268 /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.
269 /// </summary>
270 /// <typeparam name="TSource">Source sequence element type.</typeparam>
271 /// <param name="source">Source sequence.</param>
272 /// <param name="retryCount">Maximum number of retries.</param>
273 /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
274 public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source, int retryCount)
275 {
276 if (source == null)
277 throw new ArgumentNullException("source");
278 if (retryCount < 0)
279 throw new ArgumentOutOfRangeException("retryCount");
280
281 return new[] { source }.Repeat(retryCount).Catch();
282 }
283 }
284 }