Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading signature of MqttClient.ConnectAsync, MqttClientConnectResultCode expected but throws exception #1681

Closed
jschweda opened this issue Feb 20, 2023 · 1 comment · Fixed by #1749
Labels
bug Something isn't working

Comments

@jschweda
Copy link

jschweda commented Feb 20, 2023

Describe the bug

ConnectAsync returns an MqttClientConnectResult which holds an MqttClientConnectResultCode. MqttClientConnectResultCode can indicate all kinds of errors while establishing a connection. But different than expected an exception is thrown.

Which component is your bug related to?

  • Client

To Reproduce

Steps to reproduce the behavior:

  1. Using Version 4.1.4.563 of MQTTnet
  2. Run this server code
 var factory = new MqttFactory();
        var serverOptions = factory.CreateServerOptionsBuilder().WithDefaultEndpoint();
        _mqttServer = factory.CreateMqttServer(serverOptions.Build());
        _mqttServer.ValidatingConnectionAsync += (e) => {
            e.ReasonCode = MQTTnet.Protocol.MqttConnectReasonCode.BadUserNameOrPassword;
            return Task.CompletedTask;
        };
        _mqttServer.StartAsync().GetAwaiter().GetResult();
  1. Run this client code
            _mqttClient = _mqttFactory.CreateMqttClient();
            var mqttClientOptions = _optionsBuilderFactory.CreateClientOptionsBuilder().WithProtocolVersion(MqttProtocolVersion.V500);

            // throws unexpected exception
            MqttClientConnectResult result = await _mqttClient.ConnectAsync(mqttClientOptions, ct).ConfigureAwait(false);
            if (result.ResultCode != MqttClientConnectResultCode.Success)
            {
                _logErrorCannotConnect(_logger, result.ReasonString ?? "Unknown", result.ResultCode, null);
                return result;
            }
  1. ConnectAsync is not expected to throw an exception based on signature.

Expected behavior

ConnectAsync should return a MqttClientConnectResult that has the ResultCode set to a value indicating non success.

@chkr1011
Copy link
Collaborator

I added a new option to the client options so that you can choose. Otherwise it would introduce breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants