From 9661922f782126837bbe7035e70c96130d7aa7f4 Mon Sep 17 00:00:00 2001 From: bvmensvoort Date: Wed, 14 Jun 2023 21:50:13 +0200 Subject: [PATCH] Pass msg object where possible As described on https://nodered.org/docs/user-guide/writing-functions#handling-errors --- src/matrix-create-room.js | 2 +- src/matrix-crypt-file.js | 8 ++++---- src/matrix-delete-event.js | 4 ++-- src/matrix-invite-room.js | 6 +++--- src/matrix-join-room.js | 8 ++++---- src/matrix-leave-room.js | 8 ++++---- src/matrix-react.js | 10 +++++----- src/matrix-room-ban.js | 10 +++++----- src/matrix-room-kick.js | 10 +++++----- src/matrix-room-users.js | 4 ++-- src/matrix-send-file.js | 4 ++-- src/matrix-send-image.js | 4 ++-- src/matrix-send-message.js | 8 ++++---- src/matrix-synapse-create-edit-user.js | 4 ++-- src/matrix-synapse-deactivate-user.js | 4 ++-- src/matrix-synapse-join-room.js | 6 +++--- src/matrix-synapse-register.js | 6 +++--- src/matrix-synapse-users.js | 2 +- src/matrix-whois-user.js | 4 ++-- 19 files changed, 56 insertions(+), 56 deletions(-) diff --git a/src/matrix-create-room.js b/src/matrix-create-room.js index d5b7320..f9b7fa2 100644 --- a/src/matrix-create-room.js +++ b/src/matrix-create-room.js @@ -42,7 +42,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } diff --git a/src/matrix-crypt-file.js b/src/matrix-crypt-file.js index e58086d..779a1c8 100644 --- a/src/matrix-crypt-file.js +++ b/src/matrix-crypt-file.js @@ -12,22 +12,22 @@ module.exports = function(RED) { const { got } = await import('got'); if(!msg.type) { - node.error('msg.type is required.'); + node.error('msg.type is required.', msg); return; } if(!msg.content) { - node.error('msg.content is required.'); + node.error('msg.content is required.', msg); return; } if(!msg.content.file) { - node.error('msg.content.file is required.'); + node.error('msg.content.file is required.', msg); return; } if(!msg.url) { - node.error('msg.url is required.'); + node.error('msg.url is required.', msg); return; } diff --git a/src/matrix-delete-event.js b/src/matrix-delete-event.js index 187eae3..132ba16 100644 --- a/src/matrix-delete-event.js +++ b/src/matrix-delete-event.js @@ -28,7 +28,7 @@ module.exports = function(RED) { node.on('input', function(msg) { if(!msg.eventId) { - node.error("eventId is missing", {}); + node.error("eventId is missing", msg); node.send([null, msg]) return; } @@ -39,7 +39,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); return; } diff --git a/src/matrix-invite-room.js b/src/matrix-invite-room.js index dcf0689..df6afac 100644 --- a/src/matrix-invite-room.js +++ b/src/matrix-invite-room.js @@ -38,18 +38,18 @@ module.exports = function(RED) { node.on("input", function (msg) { if (! node.server || ! node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } msg.topic = node.roomId || msg.topic; if(!msg.topic) { - node.error("room must be defined in either msg.topic or in node config", {}); + node.error("room must be defined in either msg.topic or in node config", msg); return; } diff --git a/src/matrix-join-room.js b/src/matrix-join-room.js index 438a7af..a3b885e 100644 --- a/src/matrix-join-room.js +++ b/src/matrix-join-room.js @@ -25,17 +25,17 @@ module.exports = function(RED) { node.on("input", function (msg) { if (! node.server || ! node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } if(!msg.topic) { - node.error("Room must be specified in msg.topic", {}); + node.error("Room must be specified in msg.topic", msg); return; } @@ -47,7 +47,7 @@ module.exports = function(RED) { node.send([msg, null]); }) .catch(function(e){ - node.error("Error trying to join room " + msg.topic + ":" + e, {}); + node.error("Error trying to join room " + msg.topic + ":" + e, msg); msg.error = e; node.send([null, msg]); }); diff --git a/src/matrix-leave-room.js b/src/matrix-leave-room.js index 02d04a2..ecc171b 100644 --- a/src/matrix-leave-room.js +++ b/src/matrix-leave-room.js @@ -26,17 +26,17 @@ module.exports = function(RED) { node.on('input', function(msg) { if (! node.server || ! node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!msg.topic) { - node.error('No room provided in msg.topic', {}); + node.error('No room provided in msg.topic', msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } @@ -45,7 +45,7 @@ module.exports = function(RED) { node.server.matrixClient.leave(msg.topic); node.send([msg, null]); } catch(e) { - node.error("Failed to leave room " + msg.topic + ": " + e, {}); + node.error("Failed to leave room " + msg.topic + ": " + e, msg); msg.payload = e; node.send([null, msg]); } diff --git a/src/matrix-react.js b/src/matrix-react.js index 398da7f..4ce6b3d 100644 --- a/src/matrix-react.js +++ b/src/matrix-react.js @@ -27,30 +27,30 @@ module.exports = function(RED) { node.on("input", function (msg) { if (!node.server || !node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } msg.topic = node.roomId || msg.topic; if(!msg.topic) { - node.error("Room must be specified in msg.topic or in configuration", {}); + node.error("Room must be specified in msg.topic or in configuration", msg); return; } let payload = n.reaction || msg.payload; if(!payload) { - node.error('msg.payload must be defined or the reaction configured on the node.', {}); + node.error('msg.payload must be defined or the reaction configured on the node.', msg); return; } let eventId = msg.referenceEventId || msg.eventId; if(!eventId) { - node.error('Either msg.referenceEventId or msg.eventId must be defined to react to a message.', {}); + node.error('Either msg.referenceEventId or msg.eventId must be defined to react to a message.', msg); return; } diff --git a/src/matrix-room-ban.js b/src/matrix-room-ban.js index 0b3e0e5..a7d8435 100644 --- a/src/matrix-room-ban.js +++ b/src/matrix-room-ban.js @@ -27,23 +27,23 @@ module.exports = function(RED) { node.on("input", function (msg) { if (! node.server || ! node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } msg.topic = node.roomId || msg.topic; if(!msg.topic) { - node.error("Room must be specified in msg.topic or in configuration", {}); + node.error("Room must be specified in msg.topic or in configuration", msg); return; } if(!msg.userId) { - node.error("msg.userId was not set.", {}); + node.error("msg.userId was not set.", msg); return; } @@ -54,7 +54,7 @@ module.exports = function(RED) { node.send([msg, null]); }) .catch(function(e){ - node.error("Error trying to ban " + msg.userId + " from " + msg.topic, {}); + node.error("Error trying to ban " + msg.userId + " from " + msg.topic, msg); msg.error = e; node.send([null, msg]); }); diff --git a/src/matrix-room-kick.js b/src/matrix-room-kick.js index f93befe..926311b 100644 --- a/src/matrix-room-kick.js +++ b/src/matrix-room-kick.js @@ -27,23 +27,23 @@ module.exports = function(RED) { node.on("input", function (msg) { if (! node.server || ! node.server.matrixClient) { - node.error("No matrix server selected", {}); + node.error("No matrix server selected", msg); return; } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } msg.topic = node.roomId || msg.topic; if(!msg.topic) { - node.error("Room must be specified in msg.topic or in configuration", {}); + node.error("Room must be specified in msg.topic or in configuration", msg); return; } if(!msg.userId) { - node.error("msg.userId was not set.", {}); + node.error("msg.userId was not set.", msg); return; } @@ -54,7 +54,7 @@ module.exports = function(RED) { node.send([msg, null]); }) .catch(function(e){ - node.error("Error trying to kick " + msg.userId + " from " + msg.topic, {}); + node.error("Error trying to kick " + msg.userId + " from " + msg.topic, msg); msg.error = e; node.send([null, msg]); }); diff --git a/src/matrix-room-users.js b/src/matrix-room-users.js index 7df3c02..1a5347e 100644 --- a/src/matrix-room-users.js +++ b/src/matrix-room-users.js @@ -32,13 +32,13 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } let roomId = node.roomId || msg.topic; if(!roomId) { - node.error("msg.topic is required. Specify in the input or configure the room ID on the node.", {}); + node.error("msg.topic is required. Specify in the input or configure the room ID on the node.", msg); return; } diff --git a/src/matrix-send-file.js b/src/matrix-send-file.js index ffac909..ad89615 100644 --- a/src/matrix-send-file.js +++ b/src/matrix-send-file.js @@ -32,7 +32,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } @@ -58,7 +58,7 @@ module.exports = function(RED) { } if(!msg.payload) { - node.error('msg.payload is required', {}); + node.error('msg.payload is required', msg); return; } diff --git a/src/matrix-send-image.js b/src/matrix-send-image.js index b309db6..2e15e8f 100644 --- a/src/matrix-send-image.js +++ b/src/matrix-send-image.js @@ -32,7 +32,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } @@ -58,7 +58,7 @@ module.exports = function(RED) { } if(!msg.payload) { - node.error('msg.payload is required', {}); + node.error('msg.payload is required', msg); return; } diff --git a/src/matrix-send-message.js b/src/matrix-send-message.js index 6819f92..632ace5 100644 --- a/src/matrix-send-message.js +++ b/src/matrix-send-message.js @@ -72,7 +72,7 @@ module.exports = function(RED) { if(msgType === 'msg.type') { if(!msg.type) { - node.error("msg.type type is set to be passed in via msg.type but was not defined", {}); + node.error("msg.type type is set to be passed in via msg.type but was not defined", msg); return; } msgType = msg.type; @@ -80,7 +80,7 @@ module.exports = function(RED) { if(msgFormat === 'msg.format') { if(!msg.format) { - node.error("Message format is set to be passed in via msg.format but was not defined", {}); + node.error("Message format is set to be passed in via msg.format but was not defined", msg); return; } msgFormat = msg.format; @@ -92,7 +92,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); return; } @@ -105,7 +105,7 @@ module.exports = function(RED) { let payload = n.message || msg.payload; if(!payload) { - node.error('msg.payload must be defined or the message configured on the node.', {}); + node.error('msg.payload must be defined or the message configured on the node.', msg); return; } diff --git a/src/matrix-synapse-create-edit-user.js b/src/matrix-synapse-create-edit-user.js index ed1925d..43be104 100644 --- a/src/matrix-synapse-create-edit-user.js +++ b/src/matrix-synapse-create-edit-user.js @@ -42,12 +42,12 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } if(!msg.userId) { - node.error("msg.userId must be set to edit/create a user (ex: @user:server.com)", {}); + node.error("msg.userId must be set to edit/create a user (ex: @user:server.com)", msg); return; } diff --git a/src/matrix-synapse-deactivate-user.js b/src/matrix-synapse-deactivate-user.js index a881057..b6e909a 100644 --- a/src/matrix-synapse-deactivate-user.js +++ b/src/matrix-synapse-deactivate-user.js @@ -42,12 +42,12 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } if(!msg.userId) { - node.error("msg.userId must be set to edit/create a user (ex: @user:server.com)", {}); + node.error("msg.userId must be set to edit/create a user (ex: @user:server.com)", msg); return; } diff --git a/src/matrix-synapse-join-room.js b/src/matrix-synapse-join-room.js index 0cb0ce3..3fd51c2 100644 --- a/src/matrix-synapse-join-room.js +++ b/src/matrix-synapse-join-room.js @@ -41,18 +41,18 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } msg.topic = node.roomId || msg.topic; if(!msg.topic) { - node.error("room must be defined in either msg.topic or in node config", {}); + node.error("room must be defined in either msg.topic or in node config", msg); return; } if(!msg.userId) { - node.error("msg.userId is required to set user into a room", {}); + node.error("msg.userId is required to set user into a room", msg); return; } diff --git a/src/matrix-synapse-register.js b/src/matrix-synapse-register.js index 21223cf..bb6f9d7 100644 --- a/src/matrix-synapse-register.js +++ b/src/matrix-synapse-register.js @@ -27,12 +27,12 @@ module.exports = function(RED) { const { got } = await import('got'); if(!msg.payload.username) { - node.error("msg.payload.username is required", {}); + node.error("msg.payload.username is required", msg); return; } if(!msg.payload.password) { - node.error("msg.payload.password is required", {}); + node.error("msg.payload.password is required", msg); return; } @@ -52,7 +52,7 @@ module.exports = function(RED) { var nonce = response.body.nonce; if(!nonce) { - node.error('Could not get nonce from /_synapse/admin/v1/register', {}); + node.error('Could not get nonce from /_synapse/admin/v1/register', msg); return; } diff --git a/src/matrix-synapse-users.js b/src/matrix-synapse-users.js index eb1975e..f2079c6 100644 --- a/src/matrix-synapse-users.js +++ b/src/matrix-synapse-users.js @@ -31,7 +31,7 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } diff --git a/src/matrix-whois-user.js b/src/matrix-whois-user.js index 226b623..15de6d8 100644 --- a/src/matrix-whois-user.js +++ b/src/matrix-whois-user.js @@ -43,12 +43,12 @@ module.exports = function(RED) { } if(!node.server.isConnected()) { - node.error("Matrix server connection is currently closed", {}); + node.error("Matrix server connection is currently closed", msg); node.send([null, msg]); } if(!msg.userId) { - node.error("msg.userId must be set to get user whois data", {}); + node.error("msg.userId must be set to get user whois data", msg); return; }