Skip to content

Commit

Permalink
fix(mqtt): Better error handling when establishing MQTT connection
Browse files Browse the repository at this point in the history
...
  • Loading branch information
Göran Sander committed Dec 6, 2023
1 parent 2b188fb commit 6299a2c
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/udp/udp_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,20 +942,24 @@ module.exports.udpInitTaskErrorServer = () => {
// Handler for UDP error event
// eslint-disable-next-line no-unused-vars
globals.udpServerReloadTaskSocket.on('error', (message, remote) => {
const address = globals.udpServerReloadTaskSocket.address();
globals.logger.error(`TASKFAILURE: UDP server error on ${address.address}:${address.port}`);

// Publish MQTT message that UDP server has reported an error
if (globals.config.has('Butler.mqttConfig.enable') && globals.config.get('Butler.mqttConfig.enable') === true) {
if (globals?.mqttClient?.connected) {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
)}`
);
try {
const address = globals.udpServerReloadTaskSocket.address();
globals.logger.error(`TASKFAILURE: UDP server error on ${address.address}:${address.port}`);

// Publish MQTT message that UDP server has reported an error
if (globals.config.has('Butler.mqttConfig.enable') && globals.config.get('Butler.mqttConfig.enable') === true) {
if (globals?.mqttClient?.connected) {
globals.mqttClient.publish(globals.config.get('Butler.mqttConfig.taskFailureServerStatusTopic'), 'error');
} else {
globals.logger.warn(
`MQTT: MQTT client not connected. Unable to publish message to topic ${globals.config.get(
'Butler.mqttConfig.taskAbortedTopic'
)}`
);
}
}
} catch (err) {
globals.logger.error(`TASKFAILURE: Error in UDP error handler: ${err}`);
}
});

Expand Down

0 comments on commit 6299a2c

Please sign in to comment.