Reduced method complexity for sendTestEmail method

refs baccbb4942
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.
This commit is contained in:
Naz 2021-09-02 13:09:56 +04:00
parent 807322dccb
commit 26f419f085
3 changed files with 14 additions and 18 deletions

View File

@ -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);
}
}
};

View File

@ -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');
}
}
};

View File

@ -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;
};