Description
Confirm by changing [ ] to [x] below:
- I've searched for previous similar issues and didn't find any solution
Platform/OS/Hardware/Device
What are you running the sdk on?
Android
Describe the question
I am using java-v2 SDK for Android.
To establish an MQTT connection I am using the connect API of MqttClientConnection.
When the connection fails, it provides ExecutionException but is there any way by which we can know the error number or some other detail by which we can know the cause of connection error like not able to connect to the server, certificate error or any other error. So, it can be used to take appropriate action like try to recover error if possible or show user appropriate error on UI.
I have opened below issue but was not able to provide the requested detail in time and it is closed so I am creating new one.
#99
Below are some logs for different conditions.
- When Internet is not available
W/System.err: java.util.concurrent.ExecutionException: software.amazon.awssdk.crt.mqtt.MqttException: System call failure
W/System.err: at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:359)
W/System.err: at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1921)
....
W/System.err: Caused by: software.amazon.awssdk.crt.mqtt.MqttException: System call failureW/System.err: at software.amazon.awssdk.crt.mqtt.MqttClientConnection.onConnectionComplete(MqttClientConnection.java:142)
- When endpoint URL is incorrect
W/System.err: java.util.concurrent.ExecutionException: software.amazon.awssdk.crt.mqtt.MqttException: TLS (SSL) negotiation failed
W/System.err: at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:359)
W/System.err: at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1921)
....
W/System.err: Caused by: software.amazon.awssdk.crt.mqtt.MqttException: TLS (SSL) negotiation failed
W/System.err: at software.amazon.awssdk.crt.mqtt.MqttClientConnection.onConnectionComplete(MqttClientConnection.java:142)
- With invalid certificate (certificate is not valid)
W/System.err: software.amazon.awssdk.crt.CrtRuntimeException: TlsContext.tls_ctx_new: Failed to create new aws_tls_ctx (aws_last_error: AWS_IO_TLS_CTX_ERROR(1033), Failed to create tls context)
W/System.err: at software.amazon.awssdk.crt.io.TlsContext.tlsContextNew(Native Method)
W/System.err: at software.amazon.awssdk.crt.io.TlsContext.(TlsContext.java:34)
W/System.err: at software.amazon.awssdk.crt.io.ClientTlsContext.(ClientTlsContext.java:36)
W/System.err: at software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder.build(AwsIotMqttConnectionBuilder.java:401)
....
- With incorrect certificate (certificate is not allowed for connection)
W/System.err: java.util.concurrent.ExecutionException: software.amazon.awssdk.crt.mqtt.MqttException: Mqtt operation interrupted by connection shutdown
W/System.err: at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:359)
W/System.err: at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1921)
....
W/System.err: Caused by: software.amazon.awssdk.crt.mqtt.MqttException: Mqtt operation interrupted by connection shutdown
- With incorrect Root CA
W/System.err: java.util.concurrent.ExecutionException: software.amazon.awssdk.crt.mqtt.MqttException: TLS (SSL) negotiation failed
W/System.err: at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:359)
W/System.err: at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1921)
....
W/System.err: Caused by: software.amazon.awssdk.crt.mqtt.MqttException: TLS (SSL) negotiation failed
W/System.err: at software.amazon.awssdk.crt.mqtt.MqttClientConnection.onConnectionComplete(MqttClientConnection.java:142)
Please let me know in case more information required.
Activity
jmklix commentedon Oct 20, 2020
You won't be able to see the disconnect reason on the client side, because in the MQTT protocol the connection is dropped without sending a reason to the client. You can check the CloudWatch logs to see the cause of the connection failure. To enable CloudWatch logs look here.
codegame6 commentedon Oct 21, 2020
Some of the information is important for client side for user experience. Based on this information android application take required action like if certificates are invalid then it can show an indication to the user and it should not retry to connect but if it is something to do with the network then it can retry in the background and there can be other use-cases as well.
I can see an error in exception as I mentioned on the above logs. Is it possible to share the error number as well or some document saying error in each condition? So that AWS IoT Device SDK users can know the correct status and take necessary action.
codegame6 commentedon Nov 3, 2020
Is there anything already available or can be added?
jmklix commentedon Feb 10, 2021
No, there is nothing currently available. But I do agree it is something that can be added to help diagnose connection failures.
codegame6 commentedon Feb 1, 2022
Is it possible to make getErrorCode public from MqttException or provide a different approach to provide error codes? This will help us to take appropriate action based on the error codes. Currently, all the errors are reported as MqttException with different error strings which cannot be used to take actions for different errors.
4 remaining items