From 2f1894d52183a5ea978b523967dd37fb072b5edc Mon Sep 17 00:00:00 2001 From: George Talusan Date: Sat, 4 Apr 2026 00:57:29 -0400 Subject: [PATCH] catch unhandle promise rejections --- telegrambot/nodes/bot-node.js | 48 +++++++++++++++++++++++------------ telegrambot/nodes/in-node.js | 32 +++++++++++++++-------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/telegrambot/nodes/bot-node.js b/telegrambot/nodes/bot-node.js index b3b886d..111fc14 100644 --- a/telegrambot/nodes/bot-node.js +++ b/telegrambot/nodes/bot-node.js @@ -55,10 +55,14 @@ module.exports = function (RED) { let startTime = new Date().getTime(); let result = super.getUpdates(form); - result.then((updates) => { - let endTime = new Date().getTime(); - this.emit('getUpdates_end', this.cycle, endTime - startTime, updates); - }); + result + .then((updates) => { + let endTime = new Date().getTime(); + this.emit('getUpdates_end', this.cycle, endTime - startTime, updates); + }) + .catch(() => { + // Errors from getUpdates are handled by the caller; suppress unhandled rejection here. + }); return result; } @@ -383,21 +387,33 @@ module.exports = function (RED) { certificate: options.webHook.cert, }; } - newTelegramBot.setWebHook(botUrl, setWebHookOptions).then(function (success) { - if (self.verbose) { - newTelegramBot.getWebHookInfo().then(function (result) { - self.log('Webhook enabled: ' + JSON.stringify(result)); - }); - } + newTelegramBot + .setWebHook(botUrl, setWebHookOptions) + .then(function (success) { + if (self.verbose) { + newTelegramBot + .getWebHookInfo() + .then(function (result) { + self.log('Webhook enabled: ' + JSON.stringify(result)); + }) + .catch(function (err) { + self.warn('Failed to get webhook info: ' + err); + }); + } - if (success) { - self.status = 'connected'; // TODO: check if this must be SetStatus - } else { - self.abortBot('Failed to set webhook ' + botUrl, function () { + if (success) { + self.status = 'connected'; // TODO: check if this must be SetStatus + } else { + self.abortBot('Failed to set webhook ' + botUrl, function () { + self.error('Bot stopped: Webhook not set.'); + }); + } + }) + .catch(function (err) { + self.abortBot('Failed to set webhook ' + botUrl + ': ' + err, function () { self.error('Bot stopped: Webhook not set.'); }); - } - }); + }); return newTelegramBot; }; diff --git a/telegrambot/nodes/in-node.js b/telegrambot/nodes/in-node.js index ed99888..8c173ae 100644 --- a/telegrambot/nodes/in-node.js +++ b/telegrambot/nodes/in-node.js @@ -176,19 +176,29 @@ module.exports = function (RED) { // downloadable "blob" message? if (messageDetails.blob) { let fileId = msg.payload.content; - telegramBot.getFileLink(fileId).then(function (weblink) { - msg.payload.weblink = weblink; + telegramBot + .getFileLink(fileId) + .then(function (weblink) { + msg.payload.weblink = weblink; - // download and provide with path - if (config.saveDataDir) { - telegramBot.downloadFile(fileId, config.saveDataDir).then(function (path) { - msg.payload.path = path; + // download and provide with path + if (config.saveDataDir) { + telegramBot + .downloadFile(fileId, config.saveDataDir) + .then(function (path) { + msg.payload.path = path; + node.send([msg, null]); + }) + .catch(function (err) { + node.error('Failed to download file: ' + err, msg); + }); + } else { node.send([msg, null]); - }); - } else { - node.send([msg, null]); - } - }); + } + }) + .catch(function (err) { + node.error('Failed to get file link: ' + err, msg); + }); // vanilla message } else if (node.filterCommands && node.config.isCommandRegistered(messageDetails.content)) { // Do nothing