Skip to content

Commit 87fd963

Browse files
authored
Improve handling of Exception<T> (#272)
* Improve handling of Exception<T> * Small optimisation changes * Refactor GetRealTypeName and use a ConcurrentDictionary * Remove GetRealTypeName and use TypeNameHelper Added TypeNameHelper.GetTypeFullDisplayName and cache * Changes from review * Fix excess new lines
1 parent 5385301 commit 87fd963

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

src/Exceptionless/Extensions/ToErrorModelExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public static Error ToErrorModel(this Exception exception, ExceptionlessClient c
4141
private static Error ToErrorModelInternal(Exception exception, ExceptionlessClient client, bool isInner = false) {
4242
var log = client.Configuration.Resolver.GetLog();
4343
Type type = exception.GetType();
44+
string typeName = type.GetTypeDisplayName();
4445

4546
var error = new Error {
46-
Message = exception.GetMessage(),
47-
Type = type.FullName
47+
Message = exception.GetMessage(typeName),
48+
Type = typeName
4849
};
4950

5051
if (!isInner)
@@ -132,8 +133,8 @@ private static bool ValueIsEmpty(object value) {
132133
"31bf3856ad364e35"
133134
};
134135

135-
private static string GetMessage(this Exception exception) {
136-
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", exception.GetType().FullName);
136+
private static string GetMessage(this Exception exception, string typeName) {
137+
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", typeName);
137138
string message = !String.IsNullOrEmpty(exception.Message) ? exception.Message.Trim() : null;
138139
return !String.IsNullOrEmpty(message) ? message : defaultMessage;
139140
}

src/Exceptionless/Extensions/ToSimpleErrorModelExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ private static SimpleError ToSimpleErrorModelInternal(this Exception exception,
3535

3636
var log = client.Configuration.Resolver.GetLog();
3737
Type type = exception.GetType();
38+
string typeName = type.GetTypeDisplayName();
3839

3940
var error = new SimpleError {
40-
Message = GetMessage(exception),
41-
Type = type.FullName,
41+
Message = exception.GetMessage(typeName),
42+
Type = typeName,
4243
StackTrace = exception.Demystify().StackTrace
4344
};
4445

@@ -102,8 +103,8 @@ private static bool ValueIsEmpty(object value) {
102103
return false;
103104
}
104105

105-
private static string GetMessage(Exception exception) {
106-
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", exception.GetType().FullName);
106+
private static string GetMessage(this Exception exception, string typeName) {
107+
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", typeName);
107108
string message = !String.IsNullOrEmpty(exception.Message) ? exception.Message.Trim() : null;
108109

109110
return !String.IsNullOrEmpty(message) ? message : defaultMessage;

src/Exceptionless/Extensions/TypeExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,12 @@ public static bool IsNumeric(this Type type) {
118118
|| type == typeof(double)
119119
|| type == typeof(decimal);
120120
}
121+
122+
/// <summary>
123+
/// Gets the types pretty print full name.
124+
/// </summary>
125+
/// <param name="type"></param>
126+
/// <returns></returns>
127+
public static string GetTypeDisplayName(this Type type) => System.Diagnostics.TypeNameHelper.GetTypeDisplayName(type, true, true);
121128
}
122129
}

test/Exceptionless.Tests/Plugins/020_ErrorPluginTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,31 @@ public void IgnoredProperties() {
260260
Assert.Equal("{\"RandomValue\":\"Test\"}", json);
261261
}
262262
}
263+
264+
[Fact]
265+
public void WillUnwrapExceptionTypeName() {
266+
const string type = "Exceptionless.Tests.Plugins.PluginTestBase+GenericException<Exceptionless.Tests.Plugins.PluginTestBase+ErrorCategory>";
267+
268+
var errorPlugins = new List<IEventPlugin> {
269+
new ErrorPlugin(),
270+
new SimpleErrorPlugin()
271+
};
272+
273+
var client = CreateClient();
274+
foreach (var plugin in errorPlugins) {
275+
var context = new EventPluginContext(client, new Event());
276+
var exception = new GenericException<ErrorCategory>(ErrorCategory.FirstErrorBucket);
277+
var nestedException = new GenericException<ErrorCategory>(ErrorCategory.SecondErrorBucket,exception);
278+
context.ContextData.SetException(nestedException);
279+
plugin.Run(context);
280+
Assert.False(context.Cancel);
281+
Assert.Equal(Event.KnownTypes.Error, context.Event.Type);
282+
var error = context.Event.GetError();
283+
if (error != null)
284+
Assert.Equal(type, error.Type);
285+
else
286+
Assert.Equal(type, context.Event.GetSimpleError().Type);
287+
}
288+
}
263289
}
264290
}

test/Exceptionless.Tests/Plugins/PluginTestBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,16 @@ public class ExceptionWithOverriddenStackTrace : Exception {
7979
public ExceptionWithOverriddenStackTrace(string message) : base(message) { }
8080
public override string StackTrace => _stackTrace;
8181
}
82+
83+
public class GenericException<T> : Exception where T : struct {
84+
public T Value { get; }
85+
public GenericException(T value) { Value = value; }
86+
public GenericException(T value, Exception innerException) : base("", innerException) { Value = value; }
87+
}
88+
89+
public enum ErrorCategory {
90+
FirstErrorBucket,
91+
SecondErrorBucket,
92+
}
8293
}
8394
}

0 commit comments

Comments
 (0)