Removed use of deprecated new Error() syntax

refs 2f1123d6ca
refs 6f1a3e1774

- As per refed commits, we are removing deprecated use of `new Error()` in the codebase
- This bit cleans up `new Error()` usage in MEGA service
This commit is contained in:
Naz 2021-07-14 18:44:25 +04:00
parent 3b7042545a
commit f343e73c92
2 changed files with 46 additions and 4 deletions

View File

@ -23,7 +23,9 @@ const labs = require('../../../shared/labs');
const events = require('../../lib/common/events');
const messages = {
invalidSegment: 'Invalid segment value. Use one of the valid:"status:free" or "status:-free" values.'
invalidSegment: 'Invalid segment value. Use one of the valid:"status:free" or "status:-free" values.',
unexpectedEmailRecipientFilterError: 'Unexpected email_recipient_filter value "{emailRecipientFilter}", expected an NQL equivalent',
noneEmailRecipientError: 'Cannot sent email to "none" email_recipient_filter'
};
const getFromAddress = () => {
@ -129,12 +131,20 @@ const addEmail = async (postModel, options) => {
// `paid` and `free` were swapped out for NQL filters in 4.5.0, we shouldn't see them here now
case 'paid':
case 'free':
throw new Error(`Unexpected email_recipient_filter value "${emailRecipientFilter}", expected an NQL equivalent`);
throw new errors.GhostError({
message: tpl(messages.unexpectedEmailRecipientFilterError, {
emailRecipientFilter
})
});
case 'all':
filterOptions.filter = 'subscribed:true';
break;
case 'none':
throw new Error('Cannot sent email to "none" email_recipient_filter');
throw new errors.GhostError({
message: tpl(messages.noneEmailRecipientError, {
emailRecipientFilter
})
});
default:
filterOptions.filter = `subscribed:true+${emailRecipientFilter}`;
}

View File

@ -1,8 +1,40 @@
const should = require('should');
const sinon = require('sinon');
const errors = require('@tryghost/errors');
const {partitionMembersBySegment} = require('../../../../core/server/services/mega/mega');
const {addEmail, partitionMembersBySegment} = require('../../../../core/server/services/mega/mega');
describe('MEGA', function () {
describe('addEmail', function () {
it('addEmail throws when "free" or "paid" strings are used as a email_recipient_filter', async function () {
const postModel = {
get: sinon.stub().returns('free')
};
try {
await addEmail(postModel);
should.fail('addEmail did not throw');
} catch (err) {
should.equal(err instanceof errors.GhostError, true);
err.message.should.equal('Unexpected email_recipient_filter value "free", expected an NQL equivalent');
}
});
it('addEmail throws when "none" is used as a email_recipient_filter', async function () {
const postModel = {
get: sinon.stub().returns('none')
};
try {
await addEmail(postModel);
should.fail('addEmail did not throw');
} catch (err) {
should.equal(err instanceof errors.GhostError, true);
err.message.should.equal('Cannot sent email to "none" email_recipient_filter');
}
});
});
describe('partitionMembersBySegment', function () {
it('partition with no segments', function () {
const members = [{