Skip to content

Commit 89fe6b2

Browse files
committed
Fixed #139 Added SetException overload so you can submit any event type with an exception object.
You should have choice :)
1 parent f3d4ac4 commit 89fe6b2

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

src/Exceptionless/EventBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ public EventBuilder SetMessage(string message) {
6767
return this;
6868
}
6969

70+
/// <summary>
71+
/// Sets the event exception object.
72+
/// </summary>
73+
/// <param name="ex">The exception</param>
74+
public EventBuilder SetException(Exception ex) {
75+
PluginContextData.SetException(ex);
76+
return this;
77+
}
78+
7079
/// <summary>
7180
/// Sets the event geo coordinates. Can be either "lat,lon" or an IP address that will be used to auto detect the geo coordinates.
7281
/// </summary>

src/Exceptionless/Extensions/ExceptionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4+
using Exceptionless.Models;
45
using Exceptionless.Plugins;
56

67
namespace Exceptionless {
@@ -26,8 +27,7 @@ public static EventBuilder ToExceptionless(this Exception exception, ContextData
2627
pluginContextData = new ContextData();
2728

2829
pluginContextData.SetException(exception);
29-
30-
return client.CreateEvent(pluginContextData);
30+
return client.CreateEvent(pluginContextData).SetType(Event.KnownTypes.Error);
3131
}
3232
}
3333
}
@@ -55,7 +55,7 @@ public static string GetMessage(this Exception exception) {
5555

5656
return exception.GetInnermostException().Message;
5757
}
58-
58+
5959
private static readonly string _marker = "@exceptionless";
6060
public static void MarkProcessed(this Exception exception) {
6161
if (exception == null)

src/Exceptionless/Plugins/Default/020_ErrorPlugin.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ public void Run(EventPluginContext context) {
1515
context.Cancel = true;
1616
return;
1717
}
18-
19-
context.Event.Type = Event.KnownTypes.Error;
18+
19+
if (String.IsNullOrEmpty(context.Event.Type))
20+
context.Event.Type = Event.KnownTypes.Error;
21+
2022
context.Event.Data[Event.KnownDataKeys.Error] = exception.ToErrorModel(context.Client);
2123
exception.MarkProcessed();
2224
}

src/Exceptionless/Plugins/Default/020_SimpleErrorPlugin.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public void Run(EventPluginContext context) {
1515
return;
1616
}
1717

18-
context.Event.Type = Event.KnownTypes.Error;
18+
if (String.IsNullOrEmpty(context.Event.Type))
19+
context.Event.Type = Event.KnownTypes.Error;
20+
1921
context.Event.Data[Event.KnownDataKeys.SimpleError] = exception.ToSimpleErrorModel(context.Client);
2022
exception.MarkProcessed();
2123
}

tests/Exceptionless.Tests/Plugins/PluginTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,33 @@ public void ErrorPlugin_WillPreserveLineBreaks() {
327327
plugin.Run(context);
328328
Assert.False(context.Cancel);
329329

330+
Assert.Equal(Event.KnownTypes.Error, context.Event.Type);
331+
var error = context.Event.GetError();
332+
if (error != null)
333+
Assert.Equal(message, error.Message);
334+
else
335+
Assert.Equal(message, context.Event.GetSimpleError().Message);
336+
}
337+
}
338+
339+
340+
[Fact]
341+
public void ErrorPlugin_WillPreserveEventType() {
342+
const string message = "Testing";
343+
344+
var errorPlugins = new List<IEventPlugin> {
345+
new ErrorPlugin(),
346+
new SimpleErrorPlugin()
347+
};
348+
349+
var client = CreateClient();
350+
foreach (var plugin in errorPlugins) {
351+
var context = new EventPluginContext(client, new Event { Type = Event.KnownTypes.Log });
352+
context.ContextData.SetException(new NotSupportedException(message));
353+
plugin.Run(context);
354+
Assert.False(context.Cancel);
355+
356+
Assert.Equal(Event.KnownTypes.Log, context.Event.Type);
330357
var error = context.Event.GetError();
331358
if (error != null)
332359
Assert.Equal(message, error.Message);

0 commit comments

Comments
 (0)