From d4e5259fcffeaba1dfffd43f559df7d195c8e7a7 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 28 Jan 2022 15:16:36 -0500 Subject: [PATCH] fix: update usage of emailer.send to not catch (as errors are no longer thrown), email error throttler --- src/emailer.js | 8 ++++++++ src/notifications.js | 6 ------ src/socket.io/user.js | 2 +- src/user/approval.js | 2 +- src/user/bans.js | 2 +- src/user/digest.js | 6 ------ src/user/reset.js | 2 +- 7 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/emailer.js b/src/emailer.js index 25e598776c..107cb7e526 100644 --- a/src/emailer.js +++ b/src/emailer.js @@ -343,6 +343,10 @@ Emailer.sendToEmail = async (template, email, language, params) => { return true; } catch (err) { + if (Emailer._emailFailThrottle) { + return false; + } + if (err.code === 'ENOENT' && usingFallback) { Emailer.fallbackNotFound = true; winston.error(`[emailer/sendToEmail] ${await translator.translate('[[error:sendmail-not-found]]')}`); @@ -350,6 +354,10 @@ Emailer.sendToEmail = async (template, email, language, params) => { winston.error(`[emailer/sendToEmail] ${err.message || err.code || 'Unknown error while sending email.'}`); } + Emailer._emailFailThrottle = setTimeout(() => { + delete Emailer._emailFailThrottle; + }, 1000 * 60 * 5); // 5 minutes + return false; } }; diff --git a/src/notifications.js b/src/notifications.js index 6c9c01f46f..fa088c9025 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -194,7 +194,6 @@ async function pushToUids(uids, notification) { } body = posts.relativeToAbsolute(body, posts.urlRegex); body = posts.relativeToAbsolute(body, posts.imgRegex); - let errorLogged = false; await async.eachLimit(uids, 3, async (uid) => { await emailer.send('notification', uid, { path: notification.path, @@ -204,11 +203,6 @@ async function pushToUids(uids, notification) { body: body, notification: notification, showUnsubscribe: true, - }).catch((err) => { - if (!errorLogged) { - winston.error(`[emailer.send] ${err.stack}`); - errorLogged = true; - } }); }); } diff --git a/src/socket.io/user.js b/src/socket.io/user.js index 55a9a26d87..6648d1974a 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -90,7 +90,7 @@ SocketUser.reset.commit = async function (socket, data) { username: username, date: parsedDate, subject: '[[email:reset.notify.subject]]', - }).catch(err => winston.error(`[emailer.send] ${err.stack}`)); + }); }; SocketUser.isFollowing = async function (socket, data) { diff --git a/src/user/approval.js b/src/user/approval.js index 24e84fba64..029e32715a 100644 --- a/src/user/approval.js +++ b/src/user/approval.js @@ -79,7 +79,7 @@ module.exports = function (User) { subject: `[[email:welcome-to, ${meta.config.title || meta.config.browserTitle || 'NodeBB'}]]`, template: 'registration_accepted', uid: uid, - }).catch(err => winston.error(`[emailer.send] ${err.stack}`)); + }); const total = await db.incrObjectFieldBy('registration:queue:approval:times', 'totalTime', Math.floor((Date.now() - creation_time) / 60000)); const counter = await db.incrObjectField('registration:queue:approval:times', 'counter'); await db.setObjectField('registration:queue:approval:times', 'average', total / counter); diff --git a/src/user/bans.js b/src/user/bans.js index 8a7352d29e..3dcf8b3ce3 100644 --- a/src/user/bans.js +++ b/src/user/bans.js @@ -58,7 +58,7 @@ module.exports = function (User) { until: until ? (new Date(until)).toUTCString().replace(/,/g, '\\,') : false, reason: reason, }; - await emailer.send('banned', uid, data).catch(err => winston.error(`[emailer.send] ${err.stack}`)); + await emailer.send('banned', uid, data); return banData; }; diff --git a/src/user/digest.js b/src/user/digest.js index 5a23b0fd8f..ecb55c51a4 100644 --- a/src/user/digest.js +++ b/src/user/digest.js @@ -103,7 +103,6 @@ Digest.send = async function (data) { if (!data || !data.subscribers || !data.subscribers.length) { return emailsSent; } - let errorLogged = false; await batch.processArray(data.subscribers, async (uids) => { let userData = await user.getUsersFields(uids, ['uid', 'email', 'email:confirmed', 'username', 'userslug', 'lastonline']); userData = userData.filter(u => u && u.email && (meta.config.includeUnverifiedEmails || u['email:confirmed'])); @@ -142,11 +141,6 @@ Digest.send = async function (data) { popularTopics: topics.popular, interval: data.interval, showUnsubscribe: true, - }).catch((err) => { - if (!errorLogged) { - winston.error(`[user/jobs] Could not send digest email\n[emailer.send] ${err.stack}`); - errorLogged = true; - } }); })); if (data.interval !== 'alltime') { diff --git a/src/user/reset.js b/src/user/reset.js index b8a77a4d08..ad3ef60530 100644 --- a/src/user/reset.js +++ b/src/user/reset.js @@ -59,7 +59,7 @@ UserReset.send = async function (email) { subject: '[[email:password-reset-requested]]', template: 'reset', uid: uid, - }).catch(err => winston.error(`[emailer.send] ${err.stack}`)); + }); return code; };