lcarli / NodeRedIoTHub

Fork from origin plus changes
Apache License 2.0
14 stars 24 forks source link

Exception from Node Red that terminates the flow #27

Open mfisher75 opened 5 years ago

mfisher75 commented 5 years ago

In a scenario where we want to send data to an IoT device that doesn’t exist in Azure IoT Hub (using Azure IoT Hub Node) we get an Exception from the Node Red and the Flow is terminated, this behavior also occurs when using the Azure IoT Hub Registry Node when we try to add an IoT device that is already exist.

Azure IoT Hub Node:

[azureiothub:Azure IoT Hub] JSON [azureiothub:Azure IoT Hub] Connection to IoT Hub not established or configuration changed. Reconnecting. [azureiothub:Azure IoT Hub] Connecting to Azure IoT Hub: Protocol: mqtt Connection string :HostName=fleet-tests.azure-devices.net;DeviceId=testenode11;SharedAccessKey=NVYVIsTozHf3Ts4HlhWfsai18Y100JE8qtbxv86Jz58= [azureiothub:Azure IoT Hub] Could not connect: Connection refused: Not authorized [azureiothub:Azure IoT Hub] Connected to Azure IoT Hub. [azureiothub:Azure IoT Hub] Message is pending. Sending it to Azure IoT Hub. [azureiothub:Azure IoT Hub] Sending Message to Azure IoT Hub :

[red] Uncaught Exception: TypeError: Cannot read property 'sendEvent' of undefined An uncaught exception or error event has been observed. The project is configured to prevent termination of node.js in this case. Continuing activity.

It should behave as stated in the log: “The project is configured to prevent the termination of node.js in this case. Continuing activity” but this is not the case.

Azure IoT Hub Registry Node:

Nov 20 13:36:13 dataflow-westus-paas-rt0 bash[82045]: |error|Y|||moshe|iothub|sandbox|truck.azureiothubregistry:Azure IoT Hub Registry|[azureiothubregistry:Azure IoT Hub Registry] Error while trying to create a new device: DeviceAlreadyExistsError: A device with ID 'testenode10' is already registered.

We need to receive a status code in return without terminating the flow in order to proceed to another Node and handle these events.

In case of Success, for example when using the Azure IoT Hub Node for an existing IoT device we get in return:

[azureiothub:Azure IoT Hub] JSON [azureiothub:Azure IoT Hub] Connection to IoT Hub not established or configuration changed. Reconnecting. [azureiothub:Azure IoT Hub] Connecting to Azure IoT Hub: Protocol: mqtt Connection string :HostName=fleet-tests.azure-devices.net;DeviceId=testenode9;SharedAccessKey=NVYVIsTozHf3Ts4HlhWfsai18Y100JE8qtbxv86Jz58= [azureiothub:Azure IoT Hub] Connected to Azure IoT Hub. [azureiothub:Azure IoT Hub] Message is pending. Sending it to Azure IoT Hub. [azureiothub:Azure IoT Hub] Sending Message to Azure IoT Hub : Payload: "{tem: 25, wind: 20}" [azureiothub:Azure IoT Hub] Message sent. {"payload":"Message sent.","_msgid":"594b3fdd.22d58"}

It seems that if "err" exists the code performs also the "else" section:

var connectToIoTHub = function (node, pendingMessage) { node.log('Connecting to Azure IoT Hub:\n Protocol: ' + newProtocol + '\n Connection string :' + newConnectionString); client = Client.fromConnectionString(newConnectionString, Protocols[newProtocol]); client.open(function (err) { if (err) { node.error('Could not connect: ' + err.message); setStatus(node, statusEnum.disconnected); // works for me.. client = undefined; } else { node.log('Connected to Azure IoT Hub.'); setStatus(node, statusEnum.connected); // Check if a message is pending and send it if (pendingMessage) { node.log('Message is pending. Sending it to Azure IoT Hub.'); // Send the pending message sendData(node, pendingMessage); }