Open
Description
Describe the bug
The MQTT client error handler in our application is not providing sufficient error type information when connection issues occur, particularly with WebSocket connections and credential expiration. This makes it difficult to implement proper error handling and recovery strategies.
const currentCredentials = await Auth.currentCredentials();
const clientId = guffo-${Math.floor(Math.random() * 100000 + 1)}-${Date.now()}
;
const client = new mqtt.MqttClient();
const config = iot.AwsIotMqttConnectionConfigBuilder.new_builder_for_websocket()
.with_clean_session(true)
.with_client_id(clientId)
.with_endpoint(process.env.REACT_APP_AWS_ENDPOINT || '')
.with_credentials(
process.env.REACT_APP_AWS_COGNITO_REGION || '',
currentCredentials.accessKeyId,
currentCredentials.secretAccessKey,
currentCredentials.sessionToken,
)
.with_keep_alive_seconds(60)
.build();
const mqttClient = client.new_connection(config);
So when I try to catch errors with this code
mqttClient.on('error', async (error) => {
console.log('Connection error:', error);
if (navigator.onLine) await resetConnection();
});
if (!notSubscribeRef.current) {
mqttClient.on('interrupt', async () => {
console.log('Connection interrupted');
if (navigator.onLine) await resetConnection();
});
mqttClient.on('disconnect', async () => {
console.log('Disconnected from MQTT');
if (navigator.onLine) await resetConnection();
});
mqttClient.on('connection_failure', async () => {
console.log('Connection failure');
setTimeout(resetConnection, RECONNECT_DELAY);
});
}
Expected Behavior
The error handler should:
- Provide detailed error type information to distinguish between different failure scenarios
- Include specific error codes or types for common failures like:
- Credential expiration
- WebSocket connection failures
- Network connectivity issues
- Authentication failures
- Allow implementing specific handling strategies based on error types
Current Behavior
Currently, when connection issues occur:
- The
error
event handler receives a generic error object without specific error type information - We cannot distinguish between different types of failures (credential expiration, network issues, etc.)
- The error handler triggers a generic reconnection attempt without being able to handle specific cases differently
- No specific error information is provided when credentials expire or when WebSocket connection fails
Reproduction Steps
Initiate an MQTT websocket connection using cognito and wait for the connection to close on error.
Possible Solution
No response
Additional Information/Context
No response
SDK version used
SDK Version: aws-iot-device-sdk-js-v2 1.17.0
Environment details (OS name and version, etc.)
Platform: Browser (React application)
Activity