Skip to content

Commit 564d6eb

Browse files
committed
Fixes 70 Some exceptions were note being caught.
Updates to #66 as well.
1 parent 17aeb3a commit 564d6eb

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

Source/Extras/Plugins/20_ErrorPlugin.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using Exceptionless.Extensions;
23
using Exceptionless.Extras;
34
using Exceptionless.Models;
45

@@ -10,8 +11,14 @@ public void Run(EventPluginContext context) {
1011
if (exception == null)
1112
return;
1213

14+
if (exception.IsProcessed()) {
15+
context.Cancel = true;
16+
return;
17+
}
18+
1319
context.Event.Type = Event.KnownTypes.Error;
1420
context.Event.Data[Event.KnownDataKeys.Error] = exception.ToErrorModel(context.Client);
21+
exception.MarkProcessed();
1522
}
1623
}
1724
}

Source/Platforms/Mvc/ExceptionlessModule.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System.Web.Mvc;
55
using Exceptionless.Dependency;
66
using Exceptionless.Web;
7+
using Exceptionless.Web.Extensions;
78

89
namespace Exceptionless.Mvc {
910
public class ExceptionlessModule : IHttpModule {
1011
private HttpApplication _app;
1112

1213
public virtual void Init(HttpApplication app) {
1314
ExceptionlessClient.Default.Startup();
15+
ExceptionlessClient.Default.RegisterHttpApplicationErrorHandler(app);
1416
ExceptionlessClient.Default.Configuration.AddPlugin<ExceptionlessWebPlugin>();
1517
ExceptionlessClient.Default.Configuration.Resolver.Register<ILastReferenceIdManager, WebLastReferenceIdManager>();
1618

@@ -22,6 +24,7 @@ public virtual void Init(HttpApplication app) {
2224

2325
public void Dispose() {
2426
ExceptionlessClient.Default.Shutdown();
27+
ExceptionlessClient.Default.RegisterHttpApplicationErrorHandler(_app);
2528
}
2629
}
2730
}

Source/Shared/Extensions/ExceptionExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,32 @@ public static string GetMessage(this Exception exception) {
5454

5555
return exception.GetInnermostException().Message;
5656
}
57+
58+
private static readonly string _marker = "@exceptionless";
59+
public static void MarkProcessed(this Exception exception) {
60+
if (exception == null)
61+
return;
62+
63+
try {
64+
if (exception.Data != null) {
65+
var genericTypes = exception.Data.GetType().GetGenericArguments();
66+
exception.Data[_marker] = genericTypes.Length > 0 ? genericTypes[0].GetDefaultValue() : null;
67+
}
68+
} catch (Exception) { }
69+
70+
MarkProcessed(exception.InnerException);
71+
}
72+
73+
public static bool IsProcessed(this Exception exception) {
74+
if (exception == null)
75+
return false;
76+
77+
try {
78+
if (exception.Data != null && exception.Data.Contains(_marker))
79+
return true;
80+
} catch (Exception) {}
81+
82+
return IsProcessed(exception.InnerException);
83+
}
5784
}
5885
}

Source/Shared/Extensions/TypeExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ public static PropertyInfo[] GetPublicProperties(this Type type) {
3535

3636
return type.GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance);
3737
}
38+
39+
public static object GetDefaultValue(this Type type) {
40+
if (type == null || type.IsNullable())
41+
return null;
42+
43+
if (type == typeof(bool))
44+
return default(bool);
45+
if (type == typeof(byte))
46+
return default(byte);
47+
if (type == typeof(char))
48+
return default(char);
49+
if (type == typeof(decimal))
50+
return default(decimal);
51+
if (type == typeof(double))
52+
return default(double);
53+
if (type == typeof(float))
54+
return default(float);
55+
if (type == typeof(int))
56+
return default(int);
57+
if (type == typeof(long))
58+
return default(long);
59+
if (type == typeof(sbyte))
60+
return default(sbyte);
61+
if (type == typeof(uint))
62+
return default(uint);
63+
if (type == typeof(ulong))
64+
return default(ulong);
65+
if (type == typeof(ushort))
66+
return default(ushort);
67+
68+
return Activator.CreateInstance(type);
69+
}
3870

3971
public static bool IsNullable(this Type type) {
4072
if (type.IsValueType)

Source/Shared/Plugins/Default/020_SimpleErrorPlugin.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ public void Run(EventPluginContext context) {
1010
if (exception == null)
1111
return;
1212

13+
if (exception.IsProcessed()) {
14+
context.Cancel = true;
15+
return;
16+
}
17+
1318
context.Event.Type = Event.KnownTypes.Error;
1419
context.Event.Data[Event.KnownDataKeys.SimpleError] = exception.ToSimpleErrorModel(context.Client);
20+
exception.MarkProcessed();
1521
}
1622
}
1723
}

Source/Tests/Plugins/PluginTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ public void ConfigurationDefaults_IgnoredProperties() {
5858
Assert.Equal("Test", context.Event.Data["Message"]);
5959
}
6060

61+
[Fact]
62+
public void ErrorPlugin_DiscardDuplicates() {
63+
var errorPlugins = new List<IEventPlugin> {
64+
new ErrorPlugin(),
65+
new SimpleErrorPlugin()
66+
};
67+
68+
foreach (var plugin in errorPlugins) {
69+
var exception = new Exception("Nested", new MyApplicationException("Test") {
70+
IgnoredProperty = "Test",
71+
RandomValue = "Test"
72+
});
73+
74+
var client = new ExceptionlessClient();
75+
var context = new EventPluginContext(client, new Event());
76+
context.ContextData.SetException(exception);
77+
plugin.Run(context);
78+
Assert.False(context.Cancel);
79+
80+
IData error = context.Event.GetError() as IData ?? context.Event.GetSimpleError();
81+
Assert.NotNull(error);
82+
83+
context = new EventPluginContext(client, new Event());
84+
context.ContextData.SetException(exception);
85+
plugin.Run(context);
86+
Assert.True(context.Cancel);
87+
88+
error = context.Event.GetError() as IData ?? context.Event.GetSimpleError();
89+
Assert.Null(error);
90+
}
91+
}
92+
6193
[Fact]
6294
public void ErrorPlugin_IgnoredProperties() {
6395
var exception = new MyApplicationException("Test") {

0 commit comments

Comments
 (0)