Skip to content

Commit

Permalink
Merge pull request #36 from RockLib/error
Browse files Browse the repository at this point in the history
Add error event to receiver interface
  • Loading branch information
JustinMoss committed Dec 18, 2018
2 parents 06200ed + 25942a7 commit 51a75f8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions RockLib.Messaging.Tests/FakeReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class FakeReceiver : IReceiver

public event EventHandler Connected;
public event EventHandler<DisconnectedEventArgs> Disconnected;
public event EventHandler<ErrorEventArgs> Error;

public void Dispose()
{
Expand Down
31 changes: 31 additions & 0 deletions RockLib.Messaging/ErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace RockLib.Messaging
{
/// <summary>
/// Provides data for the <see cref="IReceiver.Error"/> event.
/// </summary>
public class ErrorEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="ErrorEventArgs"/> class.
/// </summary>
/// <param name="message">A message the describes the error.</param>
/// <param name="exception">The exception responsible for the error.</param>
public ErrorEventArgs(string message, Exception exception)
{
Message = message;
Exception = exception;
}

/// <summary>
/// Gets the message that describes the error.
/// </summary>
public string Message { get; }

/// <summary>
/// Gets the exception responsible for the error.
/// </summary>
public Exception Exception { get; }
}
}
13 changes: 13 additions & 0 deletions RockLib.Messaging/ForwardingReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ public event EventHandler<DisconnectedEventArgs> Disconnected
remove => Receiver.Disconnected -= value;
}

/// <summary>
/// Occurs when an error happens.
/// <para>
/// This event passes through to the <see cref="IReceiver.Error"/> event of
/// the <see cref="Receiver"/> property.
/// </para>
/// </summary>
public event EventHandler<ErrorEventArgs> Error
{
add => Receiver.Error += value;
remove => Receiver.Error -= value;
}

/// <summary>
/// Disposes the <see cref="Receiver"/> property and, if they are not null,
/// the <see cref="AcknowledgeForwarder"/>, <see cref="RollbackForwarder"/>,
Expand Down
5 changes: 5 additions & 0 deletions RockLib.Messaging/IReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ public interface IReceiver : IDisposable
/// Occurs when a connection is lost.
/// </summary>
event EventHandler<DisconnectedEventArgs> Disconnected;

/// <summary>
/// Occurs when an error happens.
/// </summary>
event EventHandler<ErrorEventArgs> Error;
}
}
12 changes: 12 additions & 0 deletions RockLib.Messaging/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public IMessageHandler MessageHandler
/// </summary>
public event EventHandler<DisconnectedEventArgs> Disconnected;

/// <summary>
/// Occurs when an error happens.
/// </summary>
public event EventHandler<ErrorEventArgs> Error;

/// <summary>
/// Frees resources and stops receiving messages.
/// </summary>
Expand All @@ -77,6 +82,13 @@ public void Dispose()
/// <param name="errorMessage">The error message that describes the reason for the disconnection.</param>
protected void OnDisconnected(string errorMessage) => Disconnected?.Invoke(this, new DisconnectedEventArgs(errorMessage));

/// <summary>
/// Invokes the <see cref="Error"/> event.
/// </summary>
/// <param name="message">A message the describes the error.</param>
/// <param name="exception">The exception responsible for the error.</param>
protected void OnError(string message, Exception exception) => Error?.Invoke(this, new ErrorEventArgs(message, exception));

/// <summary>
/// Unregisters all event handlers from the <see cref="Connected"/> and
/// <see cref="Disconnected"/> events and sets <see cref="MessageHandler"/>
Expand Down

0 comments on commit 51a75f8

Please sign in to comment.