-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
PolicyResult.cs
219 lines (192 loc) · 8.2 KB
/
PolicyResult.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
namespace Polly
{
/// <summary>
/// The captured result of executing a policy
/// </summary>
public class PolicyResult
{
internal PolicyResult(OutcomeType outcome, Exception finalException, ExceptionType? exceptionType, Context context)
{
Outcome = outcome;
FinalException = finalException;
ExceptionType = exceptionType;
Context = context;
}
/// <summary>
/// The outcome of executing the policy
/// </summary>
public OutcomeType Outcome { get; }
/// <summary>
/// The final exception captured. Will be null if policy executed successfully
/// </summary>
public Exception FinalException { get; }
/// <summary>
/// The exception type of the final exception captured. Will be null if policy executed successfully
/// </summary>
public ExceptionType? ExceptionType { get; }
/// <summary>
/// The context for this execution.
/// </summary>
public Context Context { get; }
/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a successful execution through the policy.
/// </summary>
/// <param name="context">The policy execution context</param>
/// <returns>
/// A <see cref="PolicyResult" /> representing a successful execution through the policy.
/// </returns>
public static PolicyResult Successful(Context context)
{
return new PolicyResult(OutcomeType.Successful, null, null, context);
}
/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a failed execution through the policy. />
/// </summary>
/// <param name="exception">The exception</param>
/// <param name="exceptionType">The exception type</param>
/// <param name="context">The policy execution context</param>
/// <returns>
/// A <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </returns>
public static PolicyResult Failure(Exception exception, ExceptionType exceptionType, Context context)
{
return new PolicyResult(OutcomeType.Failure, exception, exceptionType, context);
}
}
/// <summary>
/// The captured result of executing a policy
/// </summary>
public class PolicyResult<TResult>
{
internal PolicyResult(TResult result, OutcomeType outcome, Exception finalException, ExceptionType? exceptionType, Context context)
: this(result, outcome, finalException, exceptionType, default(TResult), null, context)
{
}
internal PolicyResult(TResult result, OutcomeType outcome, Exception finalException, ExceptionType? exceptionType, TResult finalHandledResult, FaultType? faultType, Context context)
{
Result = result;
Outcome = outcome;
FinalException = finalException;
ExceptionType = exceptionType;
FinalHandledResult = finalHandledResult;
FaultType = faultType;
Context = context;
}
/// <summary>
/// The outcome of executing the policy
/// </summary>
public OutcomeType Outcome { get; }
/// <summary>
/// The final exception captured. Will be null if policy executed without exception.
/// </summary>
public Exception FinalException { get; }
/// <summary>
/// The exception type of the final exception captured. Will be null if policy executed successfully
/// </summary>
public ExceptionType? ExceptionType { get; }
/// <summary>
/// The result of executing the policy. Will be default(TResult) if the policy failed
/// </summary>
public TResult Result { get; }
/// <summary>
/// The final handled result captured. Will be default(TResult) if the policy executed successfully, or terminated with an exception.
/// </summary>
public TResult FinalHandledResult { get; }
/// <summary>
/// The fault type of the final fault captured. Will be null if policy executed successfully
/// </summary>
public FaultType? FaultType { get; }
/// <summary>
/// The context for this execution.
/// </summary>
public Context Context { get; }
/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a successful execution through the policy.
/// </summary>
/// <param name="result">The result returned by execution through the policy</param>
/// <param name="context">The policy execution context</param>
/// <returns>
/// A <see cref="PolicyResult" /> representing a successful execution through the policy.
/// </returns>
public static PolicyResult<TResult> Successful(TResult result, Context context)
{
return new PolicyResult<TResult>(result, OutcomeType.Successful, null, null, context);
}
/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </summary>
/// <param name="exception">The exception</param>
/// <param name="exceptionType">The exception type</param>
/// <param name="context">The policy execution context</param>
/// <returns>
/// A <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </returns>
public static PolicyResult<TResult> Failure(Exception exception, ExceptionType exceptionType, Context context)
{
return new PolicyResult<TResult>(default(TResult), OutcomeType.Failure, exception, exceptionType, default(TResult),
exceptionType == Polly.ExceptionType.HandledByThisPolicy
? Polly.FaultType.ExceptionHandledByThisPolicy
: Polly.FaultType.UnhandledException,
context);
}
/// <summary>
/// Builds a <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </summary>
/// <param name="handledResult">The result returned by execution through the policy, which was treated as a handled failure</param>
/// <param name="context">The policy execution context</param>
/// <returns>
/// A <see cref="PolicyResult" /> representing a failed execution through the policy.
/// </returns>
public static PolicyResult<TResult> Failure(TResult handledResult, Context context)
{
return new PolicyResult<TResult>(default(TResult), OutcomeType.Failure, null, null, handledResult, Polly.FaultType.ResultHandledByThisPolicy, context);
}
}
/// <summary>
/// Represents the outcome of executing a policy
/// </summary>
public enum OutcomeType
{
/// <summary>
/// Indicates that the policy ultimately executed successfully
/// </summary>
Successful,
/// <summary>
/// Indicates that the policy ultimately failed
/// </summary>
Failure
};
/// <summary>
/// Represents the type of exception resulting from a failed policy
/// </summary>
public enum ExceptionType
{
/// <summary>
/// An exception type that has been defined to be handled by this policy
/// </summary>
HandledByThisPolicy,
/// <summary>
/// An exception type that has been not been defined to be handled by this policy
/// </summary>
Unhandled
}
/// <summary>
/// Represents the type of outcome from a failed policy
/// </summary>
public enum FaultType
{
/// <summary>
/// An exception type that has been defined to be handled by this policy
/// </summary>
ExceptionHandledByThisPolicy,
/// <summary>
/// An exception type that has been not been defined to be handled by this policy
/// </summary>
UnhandledException,
/// <summary>
/// A result value that has been defined to be handled by this policy
/// </summary>
ResultHandledByThisPolicy
}
}