Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of Exception<T> #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Exceptionless/Extensions/ToErrorModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public static Error ToErrorModel(this Exception exception, ExceptionlessClient c
private static Error ToErrorModelInternal(Exception exception, ExceptionlessClient client, bool isInner = false) {
var log = client.Configuration.Resolver.GetLog();
Type type = exception.GetType();
string typeName = type.GetTypeDisplayName();

var error = new Error {
Message = exception.GetMessage(),
Type = type.FullName
Message = exception.GetMessage(typeName),
Type = typeName
};

if (!isInner)
Expand Down Expand Up @@ -132,8 +133,8 @@ private static bool ValueIsEmpty(object value) {
"31bf3856ad364e35"
};

private static string GetMessage(this Exception exception) {
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", exception.GetType().FullName);
private static string GetMessage(this Exception exception, string typeName) {
string defaultMessage = String.Format("Exception of type '{0}' was thrown.", typeName);
string message = !String.IsNullOrEmpty(exception.Message) ? exception.Message.Trim() : null;
return !String.IsNullOrEmpty(message) ? message : defaultMessage;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Exceptionless/Extensions/ToSimpleErrorModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ private static SimpleError ToSimpleErrorModelInternal(this Exception exception,

var log = client.Configuration.Resolver.GetLog();
Type type = exception.GetType();
string typeName = type.GetTypeDisplayName();

var error = new SimpleError {
Message = GetMessage(exception),
Type = type.FullName,
Message = exception.GetMessage(typeName),
Type = typeName,
StackTrace = exception.Demystify().StackTrace
};

Expand Down Expand Up @@ -102,8 +103,8 @@ private static bool ValueIsEmpty(object value) {
return false;
}

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

return !String.IsNullOrEmpty(message) ? message : defaultMessage;
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptionless/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,12 @@ public static bool IsNumeric(this Type type) {
|| type == typeof(double)
|| type == typeof(decimal);
}

/// <summary>
/// Gets the types pretty print full name.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetTypeDisplayName(this Type type) => System.Diagnostics.TypeNameHelper.GetTypeDisplayName(type, true, true);
}
}
26 changes: 26 additions & 0 deletions test/Exceptionless.Tests/Plugins/020_ErrorPluginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,31 @@ public void IgnoredProperties() {
Assert.Equal("{\"RandomValue\":\"Test\"}", json);
}
}

[Fact]
public void WillUnwrapExceptionTypeName() {
const string type = "Exceptionless.Tests.Plugins.PluginTestBase+GenericException<Exceptionless.Tests.Plugins.PluginTestBase+ErrorCategory>";

var errorPlugins = new List<IEventPlugin> {
new ErrorPlugin(),
new SimpleErrorPlugin()
};

var client = CreateClient();
foreach (var plugin in errorPlugins) {
var context = new EventPluginContext(client, new Event());
var exception = new GenericException<ErrorCategory>(ErrorCategory.FirstErrorBucket);
var nestedException = new GenericException<ErrorCategory>(ErrorCategory.SecondErrorBucket,exception);
context.ContextData.SetException(nestedException);
plugin.Run(context);
Assert.False(context.Cancel);
Assert.Equal(Event.KnownTypes.Error, context.Event.Type);
var error = context.Event.GetError();
if (error != null)
Assert.Equal(type, error.Type);
else
Assert.Equal(type, context.Event.GetSimpleError().Type);
}
}
}
}
11 changes: 11 additions & 0 deletions test/Exceptionless.Tests/Plugins/PluginTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,16 @@ public class ExceptionWithOverriddenStackTrace : Exception {
public ExceptionWithOverriddenStackTrace(string message) : base(message) { }
public override string StackTrace => _stackTrace;
}

public class GenericException<T> : Exception where T : struct {
public T Value { get; }
public GenericException(T value) { Value = value; }
public GenericException(T value, Exception innerException) : base("", innerException) { Value = value; }
}

public enum ErrorCategory {
FirstErrorBucket,
SecondErrorBucket,
}
}
}