diff --git a/RockLib.Messaging.Tests/FakeReceiver.cs b/RockLib.Messaging.Tests/FakeReceiver.cs index dfa4903c..d36033d3 100644 --- a/RockLib.Messaging.Tests/FakeReceiver.cs +++ b/RockLib.Messaging.Tests/FakeReceiver.cs @@ -11,6 +11,7 @@ public class FakeReceiver : IReceiver public event EventHandler Connected; public event EventHandler Disconnected; + public event EventHandler Error; public void Dispose() { diff --git a/RockLib.Messaging/ErrorEventArgs.cs b/RockLib.Messaging/ErrorEventArgs.cs new file mode 100644 index 00000000..02d4a6fa --- /dev/null +++ b/RockLib.Messaging/ErrorEventArgs.cs @@ -0,0 +1,31 @@ +using System; + +namespace RockLib.Messaging +{ + /// + /// Provides data for the event. + /// + public class ErrorEventArgs : EventArgs + { + /// + /// Initializes a new instance of the class. + /// + /// A message the describes the error. + /// The exception responsible for the error. + public ErrorEventArgs(string message, Exception exception) + { + Message = message; + Exception = exception; + } + + /// + /// Gets the message that describes the error. + /// + public string Message { get; } + + /// + /// Gets the exception responsible for the error. + /// + public Exception Exception { get; } + } +} diff --git a/RockLib.Messaging/ForwardingReceiver.cs b/RockLib.Messaging/ForwardingReceiver.cs index 5c258018..97991824 100644 --- a/RockLib.Messaging/ForwardingReceiver.cs +++ b/RockLib.Messaging/ForwardingReceiver.cs @@ -193,6 +193,19 @@ public event EventHandler Disconnected remove => Receiver.Disconnected -= value; } + /// + /// Occurs when an error happens. + /// + /// This event passes through to the event of + /// the property. + /// + /// + public event EventHandler Error + { + add => Receiver.Error += value; + remove => Receiver.Error -= value; + } + /// /// Disposes the property and, if they are not null, /// the , , diff --git a/RockLib.Messaging/IReceiver.cs b/RockLib.Messaging/IReceiver.cs index 456c7f35..6e19df5a 100644 --- a/RockLib.Messaging/IReceiver.cs +++ b/RockLib.Messaging/IReceiver.cs @@ -33,5 +33,10 @@ public interface IReceiver : IDisposable /// Occurs when a connection is lost. /// event EventHandler Disconnected; + + /// + /// Occurs when an error happens. + /// + event EventHandler Error; } } \ No newline at end of file diff --git a/RockLib.Messaging/Receiver.cs b/RockLib.Messaging/Receiver.cs index cd481162..30271d89 100644 --- a/RockLib.Messaging/Receiver.cs +++ b/RockLib.Messaging/Receiver.cs @@ -52,6 +52,11 @@ public IMessageHandler MessageHandler /// public event EventHandler Disconnected; + /// + /// Occurs when an error happens. + /// + public event EventHandler Error; + /// /// Frees resources and stops receiving messages. /// @@ -77,6 +82,13 @@ public void Dispose() /// The error message that describes the reason for the disconnection. protected void OnDisconnected(string errorMessage) => Disconnected?.Invoke(this, new DisconnectedEventArgs(errorMessage)); + /// + /// Invokes the event. + /// + /// A message the describes the error. + /// The exception responsible for the error. + protected void OnError(string message, Exception exception) => Error?.Invoke(this, new ErrorEventArgs(message, exception)); + /// /// Unregisters all event handlers from the and /// events and sets