diff --git a/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttClientWrapper.cs b/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttClientWrapper.cs index 9c0d36852..d87f1f94e 100644 --- a/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttClientWrapper.cs +++ b/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttClientWrapper.cs @@ -161,6 +161,7 @@ await Task.Delay(ConnectionMonitorMillisecondsInterval, cancellationToken) } } + [SuppressMessage("", "CA1031", Justification = Justifications.ExceptionLogged)] private async Task TryConnectAsync(bool isFirstTry, CancellationToken cancellationToken) { if (_isConnected) @@ -193,7 +194,7 @@ private async Task TryConnectAsync(bool isFirstTry, CancellationToken canc await _brokerCallbacksInvoker.InvokeAsync(handler => handler.OnClientConnectedAsync(ClientConfig)) .ConfigureAwait(false); } - catch (MqttCommunicationException ex) + catch (Exception ex) { // This might happen if the client briefly connects and then immediately disconnects _logger.LogConnectError(this, ex); @@ -217,7 +218,10 @@ await MqttClient if (MqttClient is MqttClient) await Task.Delay(ConnectionCheckDelayMilliseconds, cancellationToken).ConfigureAwait(false); - return MqttClient.IsConnected; + if (!MqttClient.IsConnected) + throw new MqttConnectException("The call to ConnectAsync returned but the client is not connected."); + + return true; } catch (Exception ex) { diff --git a/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttConnectException.cs b/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttConnectException.cs new file mode 100644 index 000000000..cd7b2c198 --- /dev/null +++ b/src/Silverback.Integration.MQTT/Messaging/Broker/Mqtt/MqttConnectException.cs @@ -0,0 +1,90 @@ +// Copyright (c) 2020 Sergio Aquilini +// This code is licensed under MIT license (see LICENSE file for details) + +using System; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.Serialization; +using MQTTnet.Client; + +namespace Silverback.Messaging.Broker.Mqtt +{ + /// + /// The exception that is thrown when the MQTT client connection fails. + /// + [Serializable] + [ExcludeFromCodeCoverage] + public class MqttConnectException : SilverbackException + { + /// + /// Initializes a new instance of the class. + /// + public MqttConnectException() + { + } + + /// + /// Initializes a new instance of the class with the + /// specified message. + /// + /// + /// The exception message. + /// + public MqttConnectException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with the + /// specified message. + /// + /// + /// The exception message. + /// + /// + /// The . + /// + public MqttConnectException(string message, MqttClientPublishResult result) + : base(message) + { + Result = result; + } + + /// + /// Initializes a new instance of the class with the + /// specified message and inner exception. + /// + /// + /// The exception message. + /// + /// + /// The inner exception. + /// + public MqttConnectException(string message, Exception innerException) + : base(message, innerException) + { + } + + /// + /// Initializes a new instance of the class with the + /// serialized data. + /// + /// + /// The that holds the serialized object data about the exception being + /// thrown. + /// + /// + /// The that contains contextual information about the source or + /// destination. + /// + protected MqttConnectException(SerializationInfo info, StreamingContext context) + : base(info, context) + { + } + + /// + /// Gets the of the failed publish operation. + /// + public MqttClientPublishResult? Result { get; } + } +}