-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathErrorItem.cs
More file actions
315 lines (260 loc) · 9.27 KB
/
ErrorItem.cs
File metadata and controls
315 lines (260 loc) · 9.27 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;
namespace StackifyLib.Models
{
public class ErrorItem
{
public ErrorItem()
{
}
public ErrorItem(Exception ex)
{
try
{
var keys = ex.Data.Keys;
Data = new Dictionary<string, string>();
foreach (var item in keys)
{
if (ex.Data[item] != null)
{
Data.Add(item.ToString(), ex.Data[item].ToString());
}
}
Message = ex.Message;
#if NETFULL
if (ex is System.Data.SqlClient.SqlException)
{
System.Data.SqlClient.SqlException sql = ex as System.Data.SqlClient.SqlException;
if (sql.Errors.Count > 0 && sql.Errors[0].Number != 0)
{
ErrorTypeCode = sql.Errors[0].Number.ToString();
if (!string.IsNullOrEmpty(sql.Errors[0].Server))
{
Message = Message + "\r\nServer: " + sql.Errors[0].Server;
}
}
else
{
ErrorTypeCode = Marshal.GetHRForException(ex).ToString();
}
}
else
{
ErrorTypeCode = Marshal.GetHRForException(ex).ToString();
}
#endif
//this is the default HResult. Ignore it?
//Leave it since we are already using it. Would cause unique errors to reset on people
//if (!string.IsNullOrEmpty(ErrorTypeCode) && ErrorTypeCode == "-2146233088")
//{
// ErrorTypeCode = "";
//}
var t = ex.GetType();
ErrorType = t.FullName;
if (ex is StringException)
{
AddTraceFrames((StringException)ex);
}
else
{
AddTraceFrames(ex);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
if (ex.InnerException != null)
{
InnerError = new ErrorItem(ex.InnerException);
}
}
[JsonProperty]
public ErrorItem InnerError { get; set; }
[JsonProperty]
public List<TraceFrame> StackTrace { get; set; }
[JsonProperty]
public string Message { get; set; }
[JsonProperty]
public string ErrorType { get; set; }
[JsonProperty]
public string ErrorTypeCode { get; set; }
[JsonProperty]
public Dictionary<string, string> Data { get; set; }
[JsonProperty]
public string SourceMethod { get; set; }
private void AddTraceFrames(StringException ex)
{
StackTrace = ex.TraceFrames;
if (StackTrace != null && StackTrace.Count > 0)
{
this.SourceMethod = ex.TraceFrames[0].Method;
}
}
private void AddTraceFrames(Exception ex)
{
this.StackTrace = new List<TraceFrame>();
var stackTrace = new StackTrace(ex, true);
var errorframes = stackTrace.GetFrames();
string lastErrorFrameMethodName = null;
if (errorframes != null)
{
foreach (StackFrame frame in errorframes)
{
MethodBase method = frame.GetMethod();
var fullName = GetMethodFullName(method);
bool isSource = false;
#if NETFULL
isSource = (ex.TargetSite != null && ex.TargetSite == method);
#endif
if (isSource)
{
this.SourceMethod = fullName;
}
StackTrace.Add(new TraceFrame()
{
CodeFileName = frame.GetFileName(),
LineNum = frame.GetFileLineNumber(),
Method = fullName
});
lastErrorFrameMethodName = fullName;
}
}
#if NETFULL
var stackTrace2 = new StackTrace(true);
var allFrames = stackTrace2.GetFrames();
//logic to add missing frames not showing up in the normal exception stack trace some times
if (allFrames != null && (lastErrorFrameMethodName != null || this.SourceMethod == null))
{
bool foundLast = false;
foreach (StackFrame frame in allFrames)
{
MethodBase method = frame.GetMethod();
var fullName = GetMethodFullName(method);
if (!foundLast)
{
if (lastErrorFrameMethodName == fullName)
{
foundLast = true;
continue;
}
}
if (foundLast)
{
StackTrace.Add(new TraceFrame()
{
// LibraryName = method.Module.Name,
CodeFileName = frame.GetFileName(),
LineNum = frame.GetFileLineNumber(),
Method = GetMethodFullName(method)
});
}
}
}
#endif
}
public static string GetMethodFullName(MethodBase method, bool simpleMethodNames = false)
{
if (method == null)
return "Unknown";
#if NETFULL
if (method.ReflectedType != null)
{
if (simpleMethodNames)
{
return method.ReflectedType.FullName + "." + method.Name;
}
try
{
string fullName = method.ReflectedType.FullName + "." +
typeof(MethodBase).InvokeMember("FormatNameAndSig",
BindingFlags.NonPublic | BindingFlags.InvokeMethod |
BindingFlags.Instance, null, method, null);
if (fullName != null)
{
return fullName;
}
}
catch (Exception)
{
}
}
#endif
StringBuilder sb = new StringBuilder();
if (method.DeclaringType != null)
{
sb.Append(method.DeclaringType.FullName);
sb.Append(".");
}
sb.Append(method.Name);
sb.Append("(");
ParameterInfo[] parameters = method.GetParameters();
if (parameters != null)
{
for (int i = 0; i < parameters.Length; ++i)
{
if (0 < i)
{
sb.Append(", ");
}
ParameterInfo p = parameters[i];
if (p != null)
{
if (p.ParameterType != null)
{
sb.Append(p.ParameterType.FullName);
}
else
{
sb.Append(p.ToString());
}
}
}
}
sb.Append(")");
return sb.ToString();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}: {1}", this.ErrorType, this.Message);
ErrorItem innerError = this.InnerError;
while (innerError != null)
{
sb.AppendFormat(" ---> {0}: {1}", innerError.ErrorType, innerError.Message);
innerError = innerError.InnerError;
}
sb.Append("\r\n" + this.FramesToString());
return sb.ToString();
}
public IEnumerable<TraceFrame> GetAllFrames()
{
foreach (var item in this.StackTrace)
yield return item;
if (InnerError != null)
{
foreach (var item in InnerError.GetAllFrames())
yield return item;
}
}
public string FramesToString()
{
StringBuilder sb = new StringBuilder();
foreach (var item in this.StackTrace)
{
sb.AppendFormat(" at {0}\r\n", item.Method);
}
if (InnerError != null)
{
sb.Append("--- End of inner exception stack trace ---\r\n");
sb.Append(InnerError.FramesToString());
}
return sb.ToString();
}
}
}