From 26f419f085092a3cdce210c85c856b64a264987f Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 2 Sep 2021 13:09:56 +0400 Subject: [PATCH] Reduced method complexity for sendTestEmail method refs https://github.com/TryGhost/Ghost/commit/baccbb49429c7d69cade747160fca0179b3dfb17 refs https://github.com/TryGhost/Team/issues/694 - The change is here to remove yet another ESLint method complexity error - The custom error handling complexity was introduced here in a referenced commit without an obvious reason. The specifics of how the "sendTestEmail" method handles errors should not leak out from the method, if there are errors in the response they should be handled internally and the method would uniformly reject with a single error. --- core/server/api/canary/email-preview.js | 12 +++--------- core/server/api/v3/email-preview.js | 12 +++--------- core/server/services/mega/mega.js | 8 ++++++++ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/core/server/api/canary/email-preview.js b/core/server/api/canary/email-preview.js index c5abe7a6a1..7a5395258d 100644 --- a/core/server/api/canary/email-preview.js +++ b/core/server/api/canary/email-preview.js @@ -59,21 +59,15 @@ module.exports = { async query(frame) { const options = Object.assign(frame.options, {status: 'all'}); let model = await models.Post.findOne(options, {withRelated: ['authors']}); + if (!model) { throw new errors.NotFoundError({ message: i18n.t('errors.api.posts.postNotFound') }); } + const {emails = [], memberSegment} = frame.data; - const response = await mega.mega.sendTestEmail(model, emails, 'canary', memberSegment); - if (response && response[0] && response[0].error) { - throw new errors.EmailError({ - statusCode: response[0].error.statusCode, - message: response[0].error.message, - context: response[0].error.originalMessage - }); - } - return response; + return await mega.mega.sendTestEmail(model, emails, 'canary', memberSegment); } } }; diff --git a/core/server/api/v3/email-preview.js b/core/server/api/v3/email-preview.js index bef01a4d6b..a9f44ee2eb 100644 --- a/core/server/api/v3/email-preview.js +++ b/core/server/api/v3/email-preview.js @@ -56,21 +56,15 @@ module.exports = { async query(frame) { const options = Object.assign(frame.options, {status: 'all'}); let model = await models.Post.findOne(options, {withRelated: ['authors']}); + if (!model) { throw new errors.NotFoundError({ message: i18n.t('errors.api.posts.postNotFound') }); } + const {emails = []} = frame.data; - const response = await mega.mega.sendTestEmail(model, emails, 'v3'); - if (response && response[0] && response[0].error) { - throw new errors.EmailError({ - statusCode: response[0].error.statusCode, - message: response[0].error.message, - context: response[0].error.originalMessage - }); - } - return response; + return await mega.mega.sendTestEmail(model, emails, 'v3'); } } }; diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index 05a5a86291..e205bf41d9 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -109,6 +109,14 @@ const sendTestEmail = async (postModel, toEmails, apiVersion, memberSegment) => return Promise.reject(response.error); } + if (response && response[0] && response[0].error) { + return Promise.reject(new errors.EmailError({ + statusCode: response[0].error.statusCode, + message: response[0].error.message, + context: response[0].error.originalMessage + })); + } + return response; };