Skip to content

Commit

Permalink
feat: improve MqttClientWrapper error handling and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
BEagle1984 committed Nov 27, 2023
1 parent 2eef9e7 commit 0289308
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ await Task.Delay(ConnectionMonitorMillisecondsInterval, cancellationToken)
}
}

[SuppressMessage("", "CA1031", Justification = Justifications.ExceptionLogged)]
private async Task<bool> TryConnectAsync(bool isFirstTry, CancellationToken cancellationToken)
{
if (_isConnected)
Expand Down Expand Up @@ -193,7 +194,7 @@ private async Task<bool> TryConnectAsync(bool isFirstTry, CancellationToken canc
await _brokerCallbacksInvoker.InvokeAsync<IMqttClientConnectedCallback>(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);
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// The exception that is thrown when the MQTT client connection fails.
/// </summary>
[Serializable]
[ExcludeFromCodeCoverage]
public class MqttConnectException : SilverbackException
{
/// <summary>
/// Initializes a new instance of the <see cref="MqttConnectException" /> class.
/// </summary>
public MqttConnectException()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MqttConnectException" /> class with the
/// specified message.
/// </summary>
/// <param name="message">
/// The exception message.
/// </param>
public MqttConnectException(string message)
: base(message)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MqttConnectException" /> class with the
/// specified message.
/// </summary>
/// <param name="message">
/// The exception message.
/// </param>
/// <param name="result">
/// The <see cref="MqttClientPublishResult" />.
/// </param>
public MqttConnectException(string message, MqttClientPublishResult result)
: base(message)
{
Result = result;
}

/// <summary>
/// Initializes a new instance of the <see cref="MqttConnectException" /> class with the
/// specified message and inner exception.
/// </summary>
/// <param name="message">
/// The exception message.
/// </param>
/// <param name="innerException">
/// The inner exception.
/// </param>
public MqttConnectException(string message, Exception innerException)
: base(message, innerException)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="MqttConnectException" /> class with the
/// serialized data.
/// </summary>
/// <param name="info">
/// The <see cref="SerializationInfo" /> that holds the serialized object data about the exception being
/// thrown.
/// </param>
/// <param name="context">
/// The <see cref="StreamingContext" /> that contains contextual information about the source or
/// destination.
/// </param>
protected MqttConnectException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}

/// <summary>
/// Gets the <see cref="MqttClientPublishResult" /> of the failed publish operation.
/// </summary>
public MqttClientPublishResult? Result { get; }
}
}

0 comments on commit 0289308

Please sign in to comment.