-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
FallbackEngine.cs
49 lines (41 loc) · 1.56 KB
/
FallbackEngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Polly.Fallback
{
internal static partial class FallbackEngine
{
internal static TResult Implementation<TResult>(
Func<Context, CancellationToken, TResult> action,
Context context,
CancellationToken cancellationToken,
IEnumerable<ExceptionPredicate> shouldHandleExceptionPredicates,
IEnumerable<ResultPredicate<TResult>> shouldHandleResultPredicates,
Action<DelegateResult<TResult>, Context> onFallback,
Func<DelegateResult<TResult>, Context, CancellationToken, TResult> fallbackAction)
{
DelegateResult<TResult> delegateOutcome;
try
{
cancellationToken.ThrowIfCancellationRequested();
TResult result = action(context, cancellationToken);
if (!shouldHandleResultPredicates.Any(predicate => predicate(result)))
{
return result;
}
delegateOutcome = new DelegateResult<TResult>(result);
}
catch (Exception ex)
{
if (!shouldHandleExceptionPredicates.Any(predicate => predicate(ex)))
{
throw;
}
delegateOutcome = new DelegateResult<TResult>(ex);
}
onFallback(delegateOutcome, context);
return fallbackAction(delegateOutcome, context, cancellationToken);
}
}
}