Skip to content

Commit

Permalink
Removed use of deprecated new Error() syntax
Browse files Browse the repository at this point in the history
refs TryGhost@2f1123d
refs TryGhost@6f1a3e1

- As per refed commits, we are removing deprecated use of `new Error()` in the codebase
- This bit cleans up the rest of `new Error()` usage in MEGA service
  • Loading branch information
naz committed Jul 14, 2021
1 parent b045112 commit 5ea8e9b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
19 changes: 14 additions & 5 deletions core/server/services/mega/mega.js
Expand Up @@ -25,7 +25,9 @@ const events = require('../../lib/common/events');
const messages = {
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'
noneEmailRecipientFilterError: 'Cannot sent email to "none" email_recipient_filter',
unexpectedRecipientFilterError: 'Unexpected recipient_filter value "{recipientFilter}", expected an NQL equivalent',
noneRecipientFileterError: 'Cannot sent email to "none" recipient_filter'
};

const getFromAddress = () => {
Expand Down Expand Up @@ -141,7 +143,7 @@ const addEmail = async (postModel, options) => {
break;
case 'none':
throw new errors.GhostError({
message: tpl(messages.noneEmailRecipientError, {
message: tpl(messages.noneEmailRecipientFilterError, {
emailRecipientFilter
})
});
Expand Down Expand Up @@ -352,12 +354,18 @@ async function getEmailMemberRows({emailModel, memberSegment, 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 recipient_filter value "${recipientFilter}", expected an NQL equivalent`);
throw new errors.GhostError({
message: tpl(messages.unexpectedRecipientFilterError, {
recipientFilter
})
});
case 'all':
filterOptions.filter = 'subscribed:true';
break;
case 'none':
throw new Error('Cannot sent email to "none" recipient_filter');
throw new errors.GhostError({
message: tpl(messages.noneRecipientFileterError)
});
default:
filterOptions.filter = `subscribed:true+${recipientFilter}`;
}
Expand Down Expand Up @@ -534,7 +542,8 @@ module.exports = {
sendTestEmail,
handleUnsubscribeRequest,
// NOTE: below are only exposed for testing purposes
_partitionMembersBySegment: partitionMembersBySegment
_partitionMembersBySegment: partitionMembersBySegment,
_getEmailMemberRows: getEmailMemberRows
};

/**
Expand Down
32 changes: 31 additions & 1 deletion test/unit/services/mega/mega.test.js
Expand Up @@ -2,7 +2,7 @@ const should = require('should');
const sinon = require('sinon');
const errors = require('@tryghost/errors');

const {addEmail, _partitionMembersBySegment} = require('../../../../core/server/services/mega/mega');
const {addEmail, _partitionMembersBySegment, _getEmailMemberRows} = require('../../../../core/server/services/mega/mega');

describe('MEGA', function () {
describe('addEmail', function () {
Expand Down Expand Up @@ -35,6 +35,36 @@ describe('MEGA', function () {
});
});

describe('getEmailMemberRows', function () {
it('addEmail throws when "free" or "paid" strings are used as a recipient_filter', async function () {
const emailModel = {
get: sinon.stub().returns('paid')
};

try {
await _getEmailMemberRows({emailModel});
should.fail('getEmailMemberRows did not throw');
} catch (err) {
should.equal(err instanceof errors.GhostError, true);
err.message.should.equal('Unexpected recipient_filter value "paid", expected an NQL equivalent');
}
});

it('addEmail throws when "none" is used as a recipient_filter', async function () {
const emailModel = {
get: sinon.stub().returns('none')
};

try {
await _getEmailMemberRows({emailModel});
should.fail('getEmailMemberRows did not throw');
} catch (err) {
should.equal(err instanceof errors.GhostError, true);
err.message.should.equal('Cannot sent email to "none" recipient_filter');
}
});
});

describe('partitionMembersBySegment', function () {
it('partition with no segments', function () {
const members = [{
Expand Down

0 comments on commit 5ea8e9b

Please sign in to comment.