From 18b8eddd0d7be55406c5e7d03dc7d9108013c3f8 Mon Sep 17 00:00:00 2001 From: Sam Lord Date: Wed, 1 Dec 2021 11:22:14 +0000 Subject: [PATCH] Added linting for use of @tryghost/errors refs: https://github.com/TryGhost/Toolbox/issues/147 Errors in @tryghost/errors rely on being called with an object (with a `message` member) rather than with a string. --- core/frontend/services/apps/loader.js | 6 +++--- core/server/models/member-email-change-event.js | 4 ++-- core/server/models/member-login-event.js | 4 ++-- core/server/models/member-paid-subscription-event.js | 6 +++--- core/server/models/member-payment-event.js | 6 +++--- core/server/models/member-product-event.js | 12 ++++++------ core/server/models/member-status-event.js | 8 +++++--- core/server/models/member-subscribe-event.js | 12 +++++++++--- core/server/models/settings.js | 2 +- core/server/services/mega/mega.js | 4 +++- core/server/services/members/config.js | 2 +- core/server/services/members/service.js | 8 ++++++-- core/server/services/members/stripe-connect.js | 6 ++++-- core/shared/labs.js | 1 + package.json | 2 +- yarn.lock | 8 ++++---- 16 files changed, 54 insertions(+), 37 deletions(-) diff --git a/core/frontend/services/apps/loader.js b/core/frontend/services/apps/loader.js index cf22818aff..d87f21b5be 100644 --- a/core/frontend/services/apps/loader.js +++ b/core/frontend/services/apps/loader.js @@ -40,9 +40,9 @@ module.exports = { // Check for an activate() method on the app. if (!_.isFunction(app.activate)) { - return Promise.reject(new errors.IncorrectUsageError( - tpl(messages.noActivateMethodLoadingAppError, {name: name}) - )); + return Promise.reject(new errors.IncorrectUsageError({ + message: tpl(messages.noActivateMethodLoadingAppError, {name: name}) + })); } // Wrapping the activate() with a when because it's possible diff --git a/core/server/models/member-email-change-event.js b/core/server/models/member-email-change-event.js index 7d04cd278f..79baea49b9 100644 --- a/core/server/models/member-email-change-event.js +++ b/core/server/models/member-email-change-event.js @@ -9,11 +9,11 @@ const MemberEmailChangeEvent = ghostBookshelf.Model.extend({ } }, { async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberEmailChangeEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot edit MemberEmailChangeEvent'}); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberEmailChangeEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberEmailChangeEvent'}); } }); diff --git a/core/server/models/member-login-event.js b/core/server/models/member-login-event.js index 475d748e17..e3ea58d437 100644 --- a/core/server/models/member-login-event.js +++ b/core/server/models/member-login-event.js @@ -9,11 +9,11 @@ const MemberLoginEvent = ghostBookshelf.Model.extend({ } }, { async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberLoginEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot edit MemberLoginEvent'}); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberLoginEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberLoginEvent'}); } }); diff --git a/core/server/models/member-paid-subscription-event.js b/core/server/models/member-paid-subscription-event.js index b38747cf01..9bd0de8a8a 100644 --- a/core/server/models/member-paid-subscription-event.js +++ b/core/server/models/member-paid-subscription-event.js @@ -11,7 +11,7 @@ const MemberPaidSubscriptionEvent = ghostBookshelf.Model.extend({ customQuery(qb, options) { if (options.aggregateMRRDeltas) { if (options.limit || options.filter) { - throw new errors.IncorrectUsageError('aggregateMRRDeltas does not work when passed a filter or limit'); + throw new errors.IncorrectUsageError({message: 'aggregateMRRDeltas does not work when passed a filter or limit'}); } const knex = ghostBookshelf.knex; return qb.clear('select') @@ -33,11 +33,11 @@ const MemberPaidSubscriptionEvent = ghostBookshelf.Model.extend({ return options; }, async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberPaidSubscriptionEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot edit MemberPaidSubscriptionEvent'}); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberPaidSubscriptionEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberPaidSubscriptionEvent'}); } }); diff --git a/core/server/models/member-payment-event.js b/core/server/models/member-payment-event.js index f253a6f04f..e1f33f97fd 100644 --- a/core/server/models/member-payment-event.js +++ b/core/server/models/member-payment-event.js @@ -11,7 +11,7 @@ const MemberPaymentEvent = ghostBookshelf.Model.extend({ customQuery(qb, options) { if (options.aggregatePaymentVolume) { if (options.limit || options.filter) { - throw new errors.IncorrectUsageError('aggregatePaymentVolume does not work when passed a filter or limit'); + throw new errors.IncorrectUsageError({message: 'aggregatePaymentVolume does not work when passed a filter or limit'}); } const knex = ghostBookshelf.knex; return qb.clear('select') @@ -33,11 +33,11 @@ const MemberPaymentEvent = ghostBookshelf.Model.extend({ return options; }, async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberPaymentEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot edit MemberPaymentEvent'}); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberPaymentEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberPaymentEvent'}); } }); diff --git a/core/server/models/member-product-event.js b/core/server/models/member-product-event.js index 95b2d225e4..c74db87ce6 100644 --- a/core/server/models/member-product-event.js +++ b/core/server/models/member-product-event.js @@ -19,15 +19,15 @@ const MemberProductEvent = ghostBookshelf.Model.extend({ }, { async edit() { - throw new errors.IncorrectUsageError( - tpl(messages.cannotPerformAction, 'edit') - ); + throw new errors.IncorrectUsageError({ + message: tpl(messages.cannotPerformAction, 'edit') + }); }, async destroy() { - throw new errors.IncorrectUsageError( - tpl(messages.cannotPerformAction, 'destroy') - ); + throw new errors.IncorrectUsageError({ + message: tpl(messages.cannotPerformAction, 'destroy') + }); } }); diff --git a/core/server/models/member-status-event.js b/core/server/models/member-status-event.js index b0e04d8c4e..4ac1fc00c9 100644 --- a/core/server/models/member-status-event.js +++ b/core/server/models/member-status-event.js @@ -11,7 +11,9 @@ const MemberStatusEvent = ghostBookshelf.Model.extend({ customQuery(qb, options) { if (options.aggregateStatusCounts) { if (options.limit || options.filter) { - throw new errors.IncorrectUsageError('aggregateStatusCounts does not work when passed a filter or limit'); + throw new errors.IncorrectUsageError({ + message: 'aggregateStatusCounts does not work when passed a filter or limit' + }); } const knex = ghostBookshelf.knex; return qb.clear('select') @@ -46,11 +48,11 @@ const MemberStatusEvent = ghostBookshelf.Model.extend({ return options; }, async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberStatusEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot edit MemberStatusEvent'}); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberStatusEvent'); + throw new errors.IncorrectUsageError({message: 'Cannot destroy MemberStatusEvent'}); } }); diff --git a/core/server/models/member-subscribe-event.js b/core/server/models/member-subscribe-event.js index 5a9164f9cd..85c0735a91 100644 --- a/core/server/models/member-subscribe-event.js +++ b/core/server/models/member-subscribe-event.js @@ -11,7 +11,9 @@ const MemberSubscribeEvent = ghostBookshelf.Model.extend({ customQuery(qb, options) { if (options.aggregateSubscriptionDeltas) { if (options.limit || options.filter) { - throw new errors.IncorrectUsageError('aggregateSubscriptionDeltas does not work when passed a filter or limit'); + throw new errors.IncorrectUsageError({ + message: 'aggregateSubscriptionDeltas does not work when passed a filter or limit' + }); } const knex = ghostBookshelf.knex; return qb.clear('select') @@ -32,11 +34,15 @@ const MemberSubscribeEvent = ghostBookshelf.Model.extend({ return options; }, async edit() { - throw new errors.IncorrectUsageError('Cannot edit MemberSubscribeEvent'); + throw new errors.IncorrectUsageError({ + message: 'Cannot edit MemberSubscribeEvent' + }); }, async destroy() { - throw new errors.IncorrectUsageError('Cannot destroy MemberSubscribeEvent'); + throw new errors.IncorrectUsageError({ + message: 'Cannot destroy MemberSubscribeEvent' + }); } }); diff --git a/core/server/models/settings.js b/core/server/models/settings.js index d0040dbcba..f10254be91 100644 --- a/core/server/models/settings.js +++ b/core/server/models/settings.js @@ -346,7 +346,7 @@ Settings = ghostBookshelf.Model.extend({ ); if (validationErrors.length) { - throw new errors.ValidationError(validationErrors.join('\n')); + throw new errors.ValidationError({message: validationErrors.join('\n')}); } }, async labs(model) { diff --git a/core/server/services/mega/mega.js b/core/server/services/mega/mega.js index 621bbc5f30..5d7b93d1c4 100644 --- a/core/server/services/mega/mega.js +++ b/core/server/services/mega/mega.js @@ -416,7 +416,9 @@ function partitionMembersBySegment(memberRows, segments) { segmentedMemberRows = memberRows.filter(member => member.status !== 'free'); memberRows = memberRows.filter(member => member.status === 'free'); } else { - throw new errors.ValidationError(tpl(messages.invalidSegment)); + throw new errors.ValidationError({ + message: tpl(messages.invalidSegment) + }); } partitions[memberSegment] = segmentedMemberRows; diff --git a/core/server/services/members/config.js b/core/server/services/members/config.js index de0830aa7e..1f1da0aab7 100644 --- a/core/server/services/members/config.js +++ b/core/server/services/members/config.js @@ -98,7 +98,7 @@ class MembersConfigProvider { */ getStripeKeys(type) { if (type !== 'direct' && type !== 'connect') { - throw new errors.IncorrectUsageError(tpl(messages.incorrectKeyType)); + throw new errors.IncorrectUsageError({message: tpl(messages.incorrectKeyType)}); } const secretKey = this._settingsCache.get(`stripe_${type === 'connect' ? 'connect_' : ''}secret_key`); diff --git a/core/server/services/members/service.js b/core/server/services/members/service.js index a842b3d272..78c096dc03 100644 --- a/core/server/services/members/service.js +++ b/core/server/services/members/service.js @@ -159,12 +159,16 @@ const membersService = { } if (stripeService.api.configured && stripeService.api.mode === 'live') { - throw new errors.IncorrectUsageError(tpl(messages.noLiveKeysInDevelopment)); + throw new errors.IncorrectUsageError({ + message: tpl(messages.noLiveKeysInDevelopment) + }); } } else { const siteUrl = urlUtils.getSiteUrl(); if (!/^https/.test(siteUrl) && stripeService.api.configured) { - throw new errors.IncorrectUsageError(tpl(messages.sslRequiredForStripe)); + throw new errors.IncorrectUsageError({ + message: tpl(messages.sslRequiredForStripe) + }); } } if (!membersApi) { diff --git a/core/server/services/members/stripe-connect.js b/core/server/services/members/stripe-connect.js index 2a1ac2b095..2fd9f0769d 100644 --- a/core/server/services/members/stripe-connect.js +++ b/core/server/services/members/stripe-connect.js @@ -63,7 +63,7 @@ async function getStripeConnectTokenData(encodedData, getSessionProp) { const state = await getSessionProp(STATE_PROP); if (state !== data.s) { - throw new errors.NoPermissionError(tpl(messages.incorrectState)); + throw new errors.NoPermissionError({message: tpl(messages.incorrectState)}); } return { @@ -81,7 +81,9 @@ function checkCanConnect() { const siteUrlUsingSSL = /^https/.test(siteUrl); const cannotConnectToStripe = productionMode && !siteUrlUsingSSL; if (cannotConnectToStripe) { - throw new errors.BadRequestError('Cannot connect to stripe unless site is using https://'); + throw new errors.BadRequestError({ + message: 'Cannot connect to stripe unless site is using https://' + }); } } diff --git a/core/shared/labs.js b/core/shared/labs.js index 63c7f3e13f..02016b4b8c 100644 --- a/core/shared/labs.js +++ b/core/shared/labs.js @@ -107,6 +107,7 @@ module.exports.enabledHelper = function enabledHelper(options, callback) { }); errDetails.help = tpl(options.errorHelp || messages.errorHelp, {url: options.helpUrl}); + // eslint-disable-next-line no-restricted-syntax logging.error(new errors.DisabledFeatureError(errDetails)); const {SafeString} = require('express-hbs'); diff --git a/package.json b/package.json index 193f51a835..e78f4d942c 100644 --- a/package.json +++ b/package.json @@ -183,7 +183,7 @@ "coffeescript": "2.6.1", "cssnano": "5.0.12", "eslint": "7.32.0", - "eslint-plugin-ghost": "2.9.0", + "eslint-plugin-ghost": "2.10.0", "grunt": "1.4.1", "grunt-bg-shell": "2.3.3", "grunt-contrib-clean": "2.0.0", diff --git a/yarn.lock b/yarn.lock index 33a44b17a4..4374b71f82 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4315,10 +4315,10 @@ eslint-plugin-filenames@1.3.2: lodash.snakecase "4.1.1" lodash.upperfirst "4.3.1" -eslint-plugin-ghost@2.9.0: - version "2.9.0" - resolved "https://registry.yarnpkg.com/eslint-plugin-ghost/-/eslint-plugin-ghost-2.9.0.tgz#96992d2a240f0c41011c63107ed7686a33495401" - integrity sha512-pc/nFdYjnPmAF9Cs4BhscrE0MhWBUxQlX3gZ5XqkJOP3MqNsuYeecz85QTG6IvjarKLhr6EklVNGoQ42BaJnjA== +eslint-plugin-ghost@2.10.0: + version "2.10.0" + resolved "https://registry.yarnpkg.com/eslint-plugin-ghost/-/eslint-plugin-ghost-2.10.0.tgz#84ef189363d6c1214130145fb24894a6540adb5d" + integrity sha512-IB/DSVbyWYY3X6IatuPqSwCcQPVxxOaPOfKVQcig6ybzWPqyxh1F3qr8f1adJaY67dSmc0u+Hj6aQU75kBlVXw== dependencies: "@kapouer/eslint-plugin-no-return-in-loop" "1.0.0" eslint-plugin-ember "10.5.7"