From 99091da7c5505b6ad7806091b632969ab330cfb6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 5 Sep 2024 15:43:43 +0200 Subject: [PATCH] Improve email error logging --- server/core/lib/emailer.ts | 21 ++++++++++++++++++--- server/core/lib/job-queue/handlers/email.ts | 8 +------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/server/core/lib/emailer.ts b/server/core/lib/emailer.ts index 63bf44046..6a4fc46c2 100644 --- a/server/core/lib/emailer.ts +++ b/server/core/lib/emailer.ts @@ -271,6 +271,8 @@ class Emailer { const toEmails = arrayify(options.to) + const errors: Error[] = [] + for (const to of toEmails) { const baseOptions: SendEmailDefaultOptions = { template: 'common', @@ -292,9 +294,22 @@ class Emailer { // overridden/new variables given for a specific template in the payload const sendOptions = merge(baseOptions, options) - await email.send(sendOptions) - .then(res => logger.debug('Sent email.', { res })) - .catch(err => logger.error('Error in email sender.', { err })) + try { + const res = await email.send(sendOptions) + + logger.debug('Sent email.', { res }) + } catch (err) { + errors.push(err) + + logger.error('Error in email sender.', { err }) + } + } + + if (errors.length !== 0) { + const err = new Error('Some errors when sent emails') as Error & { errors: Error[] } + err.errors = errors + + throw err } } diff --git a/server/core/lib/job-queue/handlers/email.ts b/server/core/lib/job-queue/handlers/email.ts index 26b084e03..539a8cce1 100644 --- a/server/core/lib/job-queue/handlers/email.ts +++ b/server/core/lib/job-queue/handlers/email.ts @@ -3,15 +3,9 @@ import { EmailPayload } from '@peertube/peertube-models' import { logger } from '../../../helpers/logger.js' import { Emailer } from '../../emailer.js' -async function processEmail (job: Job) { +export function processEmail (job: Job) { const payload = job.data as EmailPayload logger.info('Processing email in job %s.', job.id) return Emailer.Instance.sendMail(payload) } - -// --------------------------------------------------------------------------- - -export { - processEmail -}