shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Subject: RangeError in mqtt5_client library - Client-side fix or library update needed? #96

Closed RaghuMudem closed 1 month ago

RaghuMudem commented 3 months ago

Dear Author,

We're incorporating the mqtt5_client library (version ^4.2.2) into our Flutter application (version 3.19.5 with Dart 3.3.3) and experiencing a non-fatal exception reported through Firebase Crashlytics.

Error:

The error message indicates a RangeError (length): Invalid value: Only valid value is 0: 1 within the MqttEnumHelper class's asString method. This suggests an attempt to access an element at an invalid index (1) in a list or similar construct that only has one element (0).

Analysis:

We've analyzed the logs and believe the issue might lie with unexpected values being passed to the asString method in the class MqttEnumHelper.

Request:

We'd appreciate your guidance on how to address this error. Ideally, we'd like to understand:

Are there potential client-side fixes we can implement to handle this unexpected value scenario? Is this a potential bug within the mqtt5_client library that could be addressed in a future update?

Additional Information:

Development Environment: Android Studio (JellyFish) IDE Code Snippet (if applicable): MqttEnumHelper.asString(..) method call. Logs:

Non-fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: RangeError (length): Invalid value: Only valid value is 0: 1.
       at MqttEnumHelper.asString(mqtt_enum_helper.dart)
       at MqttConnectionStatus.toString(mqtt_connection_status.dart:30)
       at _StringBase._interpolate(dart:core)
       at MqttSynchronousServerConnectionHandler.internalConnect(mqtt_synchronous_server_connection_handler.dart:121)
       at MqttConnectionHandlerBase.connect(mqtt_connection_handler_base.dart:108)
       at MqttServerClient.connect(mqtt_server_client.dart:95)

Sincerely, Raghu

shamblett commented 3 months ago

Yes you are correct, the error suggests that the client has received an invalid reason code somehow, I'll have a look at this but as you say this is only in a toString method so if no other solution is found I'll trap this error here and substitute a suitable error string. I should have this done over the next few days.

shamblett commented 3 months ago

OK, try it now, package re released at version 4.2.5

RaghuMudem commented 3 months ago

Hi,

Thank you for your quick response.

Updated Version Behavior:

I tested the updated version (mqtt5_client: ^4.2.5) and observed the following behavior:

Calling connect on MqttServerClient without username and password no longer throws the error I reported earlier. The connection state now simply shows as MqttConnectionState.connected without any error code or message. Previous Version Behavior:

In version 4.2.2, the same scenario correctly threw the error message as i mentioned in my question.

Expected Behavior (May be):

When attempting to connect method (MqttServerClient->connect(..)) without proper credentials, the library should ideally:

Return a descriptive error message or code indicating the issue. Set the connection state to MqttConnectionState.faulted. Attached Screenshots:

Please check the attached screen with my debug values while using version 4.2.5.

Thank you for considering this fix. It would improve the clarity of error handling in the library.

image

shamblett commented 3 months ago

OK, seems reasonable, I'll have a look at this.

shamblett commented 3 months ago

Just a question, when you say 'without proper credentials...' what do you mean. The client doesn't know you need credentials, lost of brokers work with just a client id say and no username/password, so in this case if theses weren't supplied how would the client know they are needed by the broker and take appropriate action