Skip to content

Commit

Permalink
Added posibility to set hook in case of exception thrown during sendi…
Browse files Browse the repository at this point in the history
…ng a packet
  • Loading branch information
Biegal committed Nov 28, 2014
1 parent 3546ed8 commit f2df7a6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/app/SharpRaven/RavenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ public class RavenClient : IRavenClient
{
private readonly Dsn currentDsn;
private readonly IJsonPacketFactory jsonPacketFactory;
private readonly Action<Exception> exceptionHook;


/// <summary>
/// Initializes a new instance of the <see cref="RavenClient" /> class.
/// </summary>
/// <param name="dsn">The Data Source Name in Sentry.</param>
/// <param name="jsonPacketFactory">The optional factory that will be used to create the <see cref="JsonPacket" /> that will be sent to Sentry.</param>
public RavenClient(string dsn, IJsonPacketFactory jsonPacketFactory = null)
: this(new Dsn(dsn), jsonPacketFactory)
/// <param name="exceptionHook">Hook in case of exceptions thrown during send</param>
public RavenClient(string dsn, IJsonPacketFactory jsonPacketFactory = null, Action<Exception> exceptionHook = null)
: this(new Dsn(dsn), jsonPacketFactory, exceptionHook)
{
}

Expand All @@ -72,19 +74,21 @@ public RavenClient(string dsn, IJsonPacketFactory jsonPacketFactory = null)
/// </summary>
/// <param name="dsn">The Data Source Name in Sentry.</param>
/// <param name="jsonPacketFactory">The optional factory that will be used to create the <see cref="JsonPacket" /> that will be sent to Sentry.</param>
/// <param name="exceptionHook">Hook in case of exceptions thrown during send</param>
/// <exception cref="System.ArgumentNullException">dsn</exception>
public RavenClient(Dsn dsn, IJsonPacketFactory jsonPacketFactory = null)
public RavenClient(Dsn dsn, IJsonPacketFactory jsonPacketFactory = null, Action<Exception> exceptionHook = null)
{
if (dsn == null)
throw new ArgumentNullException("dsn");

this.currentDsn = dsn;
this.jsonPacketFactory = jsonPacketFactory ?? new JsonPacketFactory();
this.exceptionHook = exceptionHook;

Logger = "root";
Timeout = TimeSpan.FromSeconds(5);
}


/// <summary>
/// Enable Gzip Compression?
/// Defaults to <c>false</c>.
Expand Down Expand Up @@ -253,6 +257,11 @@ protected virtual string Send(JsonPacket packet, Dsn dsn)

Console.WriteLine("[MESSAGE BODY] " + messageBody);
}

if (exceptionHook != null)
{
exceptionHook(exception);
}
}

return null;
Expand Down
91 changes: 91 additions & 0 deletions src/tests/SharpRaven.UnitTests/Integration/CaptureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,96 @@ public void CaptureMessage_WithFormat_ReturnsValidID()
Assert.That(id, Is.Not.Null.Or.Empty);
Assert.That(Guid.Parse(id), Is.Not.Null);
}


[Test]
public void CaptureException_Doesnt_Fail_On_Error_During_Send()
{
const string dsnUri = "http://a:b@totally.notexisting.xyz/666";

this.ravenClient = new RavenClient(dsnUri);

PrintInfo("In test client change!");
PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

try
{
FirstLevelException();
}
catch (Exception e)
{
Assert.DoesNotThrow(() => this.ravenClient.CaptureException(e));
}
}

[Test]
public void CaptureMessage_Doesnt_Fail_On_Error_During_Send()
{
const string dsnUri = "http://a:b@totally.notexisting.xyz/666";

ravenClient = new RavenClient(dsnUri);

PrintInfo("In test client change!");
PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

Assert.DoesNotThrow(() => ravenClient.CaptureMessage("Test message"));
}

[Test]
public void CaptureException_CanLogException_If_Send_Fails()
{
const string dsnUri = "http://a:b@totally.notexisting.xyz/666";

Exception hookedException = null;

this.ravenClient = new RavenClient(dsnUri, null, exp => hookedException = exp);

PrintInfo("In test client change!");
PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

try
{
FirstLevelException();
}
catch (Exception e)
{
this.ravenClient.CaptureException(e);
}

Assert.NotNull(hookedException);
}

[Test]
public void CaptureMessage__CanLogException_If_Send_Fails()
{
const string dsnUri = "http://a:b@totally.notexisting.xyz/666";

Exception hookedException = null;

ravenClient = new RavenClient(dsnUri, null, exp => hookedException = exp);

PrintInfo("In test client change!");
PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

ravenClient.CaptureMessage("Test message");

Assert.NotNull(hookedException);
}
}
}

0 comments on commit f2df7a6

Please sign in to comment.