-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathFailureDetails.cs
More file actions
264 lines (234 loc) · 12.2 KB
/
FailureDetails.cs
File metadata and controls
264 lines (234 loc) · 12.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
#nullable enable
namespace DurableTask.Core
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using DurableTask.Core.Exceptions;
using Newtonsoft.Json;
// NOTE: This class is very similar to https://github.com/microsoft/durabletask-dotnet/blob/main/src/Abstractions/TaskFailureDetails.cs.
// Any functional changes to this class should be mirrored in that class and vice versa.
/// <summary>
/// Details of an activity, orchestration, or entity operation failure.
/// </summary>
[Serializable]
public class FailureDetails : IEquatable<FailureDetails>
{
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class.
/// </summary>
/// <param name="errorType">The name of the error, which is expected to the the namespace-qualified name of the exception type.</param>
/// <param name="errorMessage">The message associated with the error, which is expected to be the exception's <see cref="Exception.Message"/> property.</param>
/// <param name="stackTrace">The exception stack trace.</param>
/// <param name="innerFailure">The inner cause of the failure.</param>
/// <param name="isNonRetriable">Whether the failure is non-retriable.</param>
/// <param name="properties">Additional properties associated with the failure.</param>
[JsonConstructor]
public FailureDetails(string errorType, string errorMessage, string? stackTrace, FailureDetails? innerFailure, bool isNonRetriable, IDictionary<string, object?>? properties = null)
{
this.ErrorType = errorType;
this.ErrorMessage = errorMessage;
this.StackTrace = stackTrace;
this.InnerFailure = innerFailure;
this.IsNonRetriable = isNonRetriable;
this.Properties = properties;
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class.
/// </summary>
/// <param name="errorType">The name of the error, which is expected to the the namespace-qualified name of the exception type.</param>
/// <param name="errorMessage">The message associated with the error, which is expected to be the exception's <see cref="Exception.Message"/> property.</param>
/// <param name="stackTrace">The exception stack trace.</param>
/// <param name="innerFailure">The inner cause of the failure.</param>
/// <param name="isNonRetriable">Whether the failure is non-retriable.</param>
public FailureDetails(string errorType, string errorMessage, string? stackTrace, FailureDetails? innerFailure, bool isNonRetriable)
: this(errorType, errorMessage, stackTrace, innerFailure, isNonRetriable, properties:null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class from an exception object.
/// </summary>
/// <param name="e">The exception used to generate the failure details.</param>
/// <param name="innerFailure">The inner cause of the failure.</param>
public FailureDetails(Exception e, FailureDetails innerFailure)
: this(e, innerFailure, properties: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class from an exception object.
/// </summary>
/// <param name="e">The exception used to generate the failure details.</param>
public FailureDetails(Exception e)
: this(e, properties: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class from an exception object.
/// </summary>
/// <param name="e">The exception used to generate the failure details.</param>
/// <param name="properties">The exception properties to include in failure details.</param>
public FailureDetails(Exception e, IDictionary<string, object?>? properties)
: this(e.GetType().FullName, GetErrorMessage(e), e.StackTrace, FromException(e.InnerException), false, properties)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class from an exception object.
/// </summary>
/// <param name="e">The exception used to generate the failure details.</param>
/// <param name="innerFailure">The inner cause of the failure.</param>
/// <param name="properties">The exception properties to include in failure details.</param>
public FailureDetails(Exception e, FailureDetails innerFailure, IDictionary<string, object?>? properties)
: this(e.GetType().FullName, GetErrorMessage(e), e.StackTrace, innerFailure, false, properties)
{
}
/// <summary>
/// For testing purposes only: Initializes a new, empty instance of the <see cref="FailureDetails"/> class.
/// </summary>
public FailureDetails()
{
this.ErrorType = "None";
this.ErrorMessage = string.Empty;
this.Properties = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="FailureDetails"/> class from a serialization context.
/// </summary>
protected FailureDetails(SerializationInfo info, StreamingContext context)
{
this.ErrorType = info.GetString(nameof(this.ErrorType));
this.ErrorMessage = info.GetString(nameof(this.ErrorMessage));
this.StackTrace = info.GetString(nameof(this.StackTrace));
this.InnerFailure = (FailureDetails)info.GetValue(nameof(this.InnerFailure), typeof(FailureDetails));
// Handle backward compatibility for Properties property - defaults to null
try
{
this.Properties = (IDictionary<string, object?>?)info.GetValue(nameof(this.Properties), typeof(IDictionary<string, object?>));
}
catch (SerializationException)
{
// Default to null for backward compatibility
this.Properties = null;
}
}
/// <summary>
/// Gets the type of the error, which is expected to the exception type's <see cref="Type.FullName"/> value.
/// </summary>
public string ErrorType { get; }
/// <summary>
/// Gets the message associated with the error, which is expected to be the exception's <see cref="Exception.Message"/> property.
/// </summary>
public string ErrorMessage { get; }
/// <summary>
/// Gets the exception stack trace.
/// </summary>
public string? StackTrace { get; }
/// <summary>
/// Gets the inner cause of this failure.
/// </summary>
public FailureDetails? InnerFailure { get; }
/// <summary>
/// Gets a value indicating whether this failure is non-retriable, meaning it should not be retried.
/// </summary>
public bool IsNonRetriable { get; }
/// <summary>
/// Gets additional properties associated with the failure.
/// </summary>
public IDictionary<string, object?>? Properties { get; }
/// <summary>
/// Gets a debug-friendly description of the failure information.
/// </summary>
public override string ToString()
{
return $"{this.ErrorType}: {this.ErrorMessage}";
}
/// <summary>
/// Returns <c>true</c> if the task failure was provided by the specified exception type.
/// </summary>
/// <remarks>
/// This method allows checking if a task failed due to an exception of a specific type by attempting
/// to load the type specified in <see cref="ErrorType"/>. If the exception type cannot be loaded
/// for any reason, this method will return <c>false</c>. Base types are supported.
/// </remarks>
/// <typeparam name="T">The type of exception to test against.</typeparam>
/// <returns>Returns <c>true</c> if the <see cref="ErrorType"/> value matches <typeparamref name="T"/>; <c>false</c> otherwise.</returns>
public bool IsCausedBy<T>() where T : Exception
{
// This check works for .NET exception types defined in System.Core.PrivateLib (aka mscorelib.dll)
Type? exceptionType = Type.GetType(this.ErrorType, throwOnError: false);
// For exception types defined in the same assembly as the target exception type.
exceptionType ??= typeof(T).Assembly.GetType(this.ErrorType, throwOnError: false);
// For custom exception types defined in the app's assembly.
exceptionType ??= Assembly.GetCallingAssembly().GetType(this.ErrorType, throwOnError: false);
if (exceptionType == null)
{
// This last check works for exception types defined in any loaded assembly (e.g. NuGet packages, etc.).
// This is a fallback that should rarely be needed except in obscure cases.
List<Type> matchingExceptionTypes = AppDomain.CurrentDomain.GetAssemblies()
.Select(a => a.GetType(this.ErrorType, throwOnError: false))
.Where(t => t is not null)
.ToList();
// Previously, this logic would only return true if matchingExceptionTypes found only one assembly with a type matching ErrorType.
// Now, it will return true if any matching assembly has a type that is assignable to T.
return matchingExceptionTypes.Any(matchType => typeof(T).IsAssignableFrom(matchType));
}
return exceptionType != null && typeof(T).IsAssignableFrom(exceptionType);
}
/// <summary>
/// Gets whether two <see cref="FailureDetails"/> objects are equivalent using value semantics.
/// </summary>
public override bool Equals(object other) => Equals(other as FailureDetails);
/// <summary>
/// Gets whether two <see cref="FailureDetails"/> objects are equivalent using value semantics.
/// </summary>
public bool Equals(FailureDetails? other)
{
if (ReferenceEquals(other, null))
{
return false;
}
return
this.ErrorType == other.ErrorType &&
this.ErrorMessage == other.ErrorMessage &&
this.StackTrace == other.StackTrace &&
this.InnerFailure == other.InnerFailure;
}
/// <inheritdoc/>
public override int GetHashCode()
{
return (ErrorType, ErrorMessage, StackTrace, InnerFailure).GetHashCode();
}
static string GetErrorMessage(Exception e)
{
if (e is TaskFailedException tfe)
{
return $"Task '{tfe.Name}' (#{tfe.ScheduleId}) failed with an unhandled exception: {tfe.Message}";
}
else
{
return e.Message;
}
}
static FailureDetails? FromException(Exception? e)
{
return FromException(e, properties : null);
}
static FailureDetails? FromException(Exception? e, IDictionary<string, object?>? properties)
{
return e == null ? null : new FailureDetails(e, properties : properties);
}
}
}