Skip to content

Commit

Permalink
Merge pull request #84 from bugsnag/martin308/update-severity
Browse files Browse the repository at this point in the history
Callback severity changes
  • Loading branch information
martin308 committed Mar 29, 2018
2 parents b3ed1e9 + af09dbf commit 083ef81
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

## Unreleased

### Bug fixes

* Allow the severity to be changed in callbacks
| [martin308](https://github.com/martin308)
| [#84](https://github.com/bugsnag/bugsnag-dotnet/pull/84)

## 2.0.1 (2018-03-27)

### Bug fixes
Expand Down
34 changes: 28 additions & 6 deletions src/Bugsnag/Payload/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Bugsnag.Payload
/// </summary>
public class Event : Dictionary<string, object>
{
public Event(string payloadVersion, App app, Device device, System.Exception exception, HandledState severity, IEnumerable<Breadcrumb> breadcrumbs, Session session)
private HandledState _handledState;

public Event(string payloadVersion, App app, Device device, System.Exception exception, HandledState handledState, IEnumerable<Breadcrumb> breadcrumbs, Session session)
{
this.AddToPayload("payloadVersion", payloadVersion);
this.AddToPayload("exceptions", new Exceptions(exception, 5).ToArray());
Expand All @@ -17,11 +19,7 @@ public Event(string payloadVersion, App app, Device device, System.Exception exc
this.AddToPayload("metaData", new Metadata());
this.AddToPayload("breadcrumbs", breadcrumbs);
this.AddToPayload("session", session);

foreach (var item in severity)
{
this[item.Key] = item.Value;
}
HandledState = handledState;
}

public bool IsHandled
Expand Down Expand Up @@ -87,6 +85,30 @@ public IEnumerable<Breadcrumb> Breadcrumbs
get { return this.Get("breadcrumbs") as IEnumerable<Breadcrumb>; }
}

public Severity Severity
{
set
{
HandledState = HandledState.ForCallbackSpecifiedSeverity(value, _handledState);
}
get
{
return _handledState.Severity;
}
}

private HandledState HandledState
{
set
{
_handledState = value;
foreach (var item in value)
{
this[item.Key] = item.Value;
}
}
}

/// <summary>
/// Called if the payload size is too large to send, removes data so that the payload
/// can be sent succesfully.
Expand Down
5 changes: 5 additions & 0 deletions src/Bugsnag/Payload/HandledState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ public static HandledState ForCallbackSpecifiedSeverity(Bugsnag.Severity severit
return new HandledState(previousSeverity.Handled, severity, SeverityReason.ForCallbackSpecifiedSeverity());
}

private readonly Severity _severity;

public Severity Severity => _severity;

HandledState(bool handled, Bugsnag.Severity severity, SeverityReason reason)
{
_severity = severity;
this.AddToPayload("unhandled", !handled);
this.AddToPayload("severityReason", reason);

Expand Down
41 changes: 41 additions & 0 deletions tests/Bugsnag.Tests/Payload/EventTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Bugsnag.Payload;
using Xunit;
Expand Down Expand Up @@ -40,5 +42,44 @@ public void SeverityKeysAreAddedCorrectly()
Assert.Contains(key, @event.Keys);
}
}

[Theory]
[InlineData(Severity.Error)]
[InlineData(Severity.Warning)]
[InlineData(Severity.Info)]
public void SeverityCanBeRetrieved(Severity severity)
{
var app = new App("version", "releaseStage", "type");
var device = new Device("hostname");
var exception = new System.DllNotFoundException();
var breadcrumbs = Enumerable.Empty<Breadcrumb>();
var session = new Session();
var handledState = HandledState.ForUserSpecifiedSeverity(severity);

var @event = new Event("1", app, device, exception, handledState, breadcrumbs, session);

Assert.Equal(severity, @event.Severity);
}

[Theory]
[InlineData(Severity.Error, Severity.Info)]
[InlineData(Severity.Warning, Severity.Error)]
[InlineData(Severity.Info, Severity.Warning)]
public void SeverityCanBeUpdated(Severity originalSeverity, Severity updatedSeverity)
{
var app = new App("version", "releaseStage", "type");
var device = new Device("hostname");
var exception = new System.DllNotFoundException();
var breadcrumbs = Enumerable.Empty<Breadcrumb>();
var session = new Session();
var handledState = HandledState.ForUserSpecifiedSeverity(originalSeverity);

var @event = new Event("1", app, device, exception, handledState, breadcrumbs, session);

@event.Severity = updatedSeverity;

Assert.Equal(updatedSeverity, @event.Severity);
Assert.Contains("severityReason", @event.Keys);
}
}
}
10 changes: 8 additions & 2 deletions tests/Bugsnag.Tests/Payload/HandledStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void UnhandledExceptionPayloadIsCorrect()

Assert.True((bool)handledState["unhandled"]);
Assert.Equal("error", handledState["severity"]);
Assert.Equal(Severity.Error, handledState.Severity);
}

[Fact]
Expand All @@ -21,25 +22,30 @@ public void HandledExceptionPayloadIsCorrect()

Assert.False((bool)handledState["unhandled"]);
Assert.Equal("warning", handledState["severity"]);
Assert.Equal(Severity.Warning, handledState.Severity);
}

[Fact]
public void UserSpecifiedSeverityPayloadIsCorrect()
{
var handledState = HandledState.ForUserSpecifiedSeverity(Severity.Warning);
const Severity warning = Severity.Warning;
var handledState = HandledState.ForUserSpecifiedSeverity(warning);

Assert.False((bool)handledState["unhandled"]);
Assert.Equal("warning", handledState["severity"]);
Assert.Equal(warning, handledState.Severity);
}

[Fact]
public void CallbackSpecifiedSeverityPayloadIsCorrect()
{
var originalHandledState = HandledState.ForUnhandledException();
var handledState = HandledState.ForCallbackSpecifiedSeverity(Severity.Info, originalHandledState);
const Severity info = Severity.Info;
var handledState = HandledState.ForCallbackSpecifiedSeverity(info, originalHandledState);

Assert.True((bool)handledState["unhandled"]); // same as the original severity
Assert.Equal("info", handledState["severity"]);
Assert.Equal(info, handledState.Severity);
}
}
}

0 comments on commit 083ef81

Please sign in to comment.