Skip to content

Commit 3ab4f91

Browse files
committed
Fixes #14 ex.ToExceptionless() can fail loading type info
1 parent fe1c035 commit 3ab4f91

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

Source/Extras/Extensions/ToErrorModelExtensions.cs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,21 @@ private static Error ToErrorModelInternal(Exception exception, IExceptionlessLog
4343
if (!isInner)
4444
error.Modules = GetLoadedModules(log);
4545

46-
error.PopulateStackTrace(error, exception);
46+
error.PopulateStackTrace(error, exception, log);
4747

4848
try {
4949
PropertyInfo info = type.GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Instance);
5050
if (info != null)
5151
error.Code = info.GetValue(exception, null).ToString();
5252
} catch (Exception) { }
5353

54-
if (exception.TargetSite != null) {
55-
error.TargetMethod = new Method();
56-
error.TargetMethod.PopulateMethod(error, exception.TargetSite);
54+
try {
55+
if (exception.TargetSite != null) {
56+
error.TargetMethod = new Method();
57+
error.TargetMethod.PopulateMethod(error, exception.TargetSite);
58+
}
59+
} catch (Exception ex) {
60+
log.Error(typeof(ExceptionlessClient), ex, "Error populating TargetMethod: " + ex.Message);
5761
}
5862

5963
try {
@@ -110,45 +114,49 @@ private static string GetMessage(this Exception exception) {
110114
private static ModuleCollection GetLoadedModules(IExceptionlessLog log, bool includeSystem = false, bool includeDynamic = false) {
111115
var modules = new ModuleCollection();
112116

113-
int id = 1;
114-
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
115-
if (!includeDynamic && assembly.IsDynamic)
116-
continue;
117-
118-
try {
119-
if (!includeDynamic && String.IsNullOrEmpty(assembly.Location))
117+
try {
118+
int id = 1;
119+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
120+
if (!includeDynamic && assembly.IsDynamic)
120121
continue;
121-
} catch (SecurityException ex) {
122-
const string message = "An error occurred while getting the Assembly.Location value. This error will occur when when you are not running under full trust.";
123-
log.Error(typeof(ExceptionlessClient), ex, message);
124-
}
125122

126-
if (!includeSystem) {
127123
try {
128-
string publicKeyToken = assembly.GetAssemblyName().GetPublicKeyToken().ToHex();
129-
if (_msPublicKeyTokens.Contains(publicKeyToken))
130-
continue;
131-
132-
object[] attrs = assembly.GetCustomAttributes(typeof(GeneratedCodeAttribute), true);
133-
if (attrs.Length > 0)
124+
if (!includeDynamic && String.IsNullOrEmpty(assembly.Location))
134125
continue;
135-
} catch {}
136-
}
137-
138-
var module = assembly.ToModuleInfo();
139-
if (module.ModuleId > 0)
140-
continue;
126+
} catch (SecurityException ex) {
127+
const string message = "An error occurred while getting the Assembly.Location value. This error will occur when when you are not running under full trust.";
128+
log.Error(typeof(ExceptionlessClient), ex, message);
129+
}
130+
131+
if (!includeSystem) {
132+
try {
133+
string publicKeyToken = assembly.GetAssemblyName().GetPublicKeyToken().ToHex();
134+
if (_msPublicKeyTokens.Contains(publicKeyToken))
135+
continue;
136+
137+
object[] attrs = assembly.GetCustomAttributes(typeof(GeneratedCodeAttribute), true);
138+
if (attrs.Length > 0)
139+
continue;
140+
} catch {}
141+
}
142+
143+
var module = assembly.ToModuleInfo();
144+
if (module.ModuleId > 0)
145+
continue;
141146

142-
module.ModuleId = id;
143-
modules.Add(module);
147+
module.ModuleId = id;
148+
modules.Add(module);
144149

145-
id++;
150+
id++;
151+
}
152+
} catch (Exception ex) {
153+
log.Error(typeof(ExceptionlessClient), ex, "Error loading modules: " + ex.Message);
146154
}
147155

148156
return modules;
149157
}
150158

151-
private static void PopulateStackTrace(this Error error, Error root, Exception exception) {
159+
private static void PopulateStackTrace(this Error error, Error root, Exception exception, IExceptionlessLog log) {
152160
StackFrame[] frames = null;
153161
try {
154162
var st = new StackTrace(exception, true);
@@ -159,7 +167,7 @@ private static void PopulateStackTrace(this Error error, Error root, Exception e
159167
return;
160168

161169
foreach (StackFrame frame in frames) {
162-
var stackFrame = new Exceptionless.Models.Data.StackFrame {
170+
var stackFrame = new Models.Data.StackFrame {
163171
LineNumber = frame.GetFileLineNumber(),
164172
Column = frame.GetFileColumnNumber(),
165173
FileName = frame.GetFileName()
@@ -168,7 +176,11 @@ private static void PopulateStackTrace(this Error error, Error root, Exception e
168176
stackFrame.Data["ILOffset"] = frame.GetILOffset();
169177
stackFrame.Data["NativeOffset"] = frame.GetNativeOffset();
170178

171-
stackFrame.PopulateMethod(root, frame.GetMethod());
179+
try {
180+
stackFrame.PopulateMethod(root, frame.GetMethod());
181+
} catch (Exception ex) {
182+
log.Error(typeof(ExceptionlessClient), ex, "Error populating StackFrame method info: " + ex.Message);
183+
}
172184

173185
error.StackTrace.Add(stackFrame);
174186
}
@@ -181,7 +193,7 @@ private static void PopulateMethod(this Method method, Error root, MethodBase me
181193
method.Name = methodBase.Name;
182194
if (methodBase.DeclaringType != null) {
183195
method.DeclaringNamespace = methodBase.DeclaringType.Namespace;
184-
if (methodBase.DeclaringType.MemberType == MemberTypes.NestedType)
196+
if (methodBase.DeclaringType.MemberType == MemberTypes.NestedType && methodBase.DeclaringType.DeclaringType != null)
185197
method.DeclaringType = methodBase.DeclaringType.DeclaringType.Name + "+" + methodBase.DeclaringType.Name;
186198
else
187199
method.DeclaringType = methodBase.DeclaringType.Name;

0 commit comments

Comments
 (0)