From 91df910dbbc022f1699e66bf7f165989159d5b68 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Mon, 24 Jan 2022 16:38:16 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20crashing=20on=20boot=20w?= =?UTF-8?q?ith=20revoked=20Stripe=20keys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit no-issue The refactor of Stripe boot logic missed catching any errors from the migrations running or the webhooks initialising. This adds try/catches to the services so that we can log the errors. --- core/server/services/members/service.js | 6 ++- core/server/services/stripe/index.js | 54 +---------------------- core/server/services/stripe/service.js | 58 +++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 core/server/services/stripe/service.js diff --git a/core/server/services/members/service.js b/core/server/services/members/service.js index 7b3a63afd2..46b2d522de 100644 --- a/core/server/services/members/service.js +++ b/core/server/services/members/service.js @@ -148,7 +148,11 @@ module.exports = { } })(); - await stripeService.migrations.execute(); + try { + await stripeService.migrations.execute(); + } catch (err) { + logging.error(err); + } }, contentGating: require('./content-gating'), diff --git a/core/server/services/stripe/index.js b/core/server/services/stripe/index.js index 2ebae778d3..102ef66d4f 100644 --- a/core/server/services/stripe/index.js +++ b/core/server/services/stripe/index.js @@ -1,53 +1 @@ -const _ = require('lodash'); -const StripeService = require('@tryghost/members-stripe-service'); -const membersService = require('../members'); -const config = require('../../../shared/config'); -const settings = require('../../../shared/settings-cache'); -const urlUtils = require('../../../shared/url-utils'); -const events = require('../../lib/common/events'); -const models = require('../../models'); -const {getConfig} = require('./config'); - -function configureApi() { - const cfg = getConfig(settings, config, urlUtils); - if (cfg) { - module.exports.configure(cfg); - return true; - } - return false; -} - -const debouncedConfigureApi = _.debounce(() => { - configureApi(); -}, 600); - -module.exports = new StripeService({ - membersService, - models: _.pick(models, ['Product', 'StripePrice', 'StripeCustomerSubscription', 'StripeProduct', 'MemberStripeCustomer', 'Offer', 'Settings']), - StripeWebhook: { - async get() { - return { - webhook_id: settings.get('members_stripe_webhook_id'), - secret: settings.get('members_stripe_webhook_secret') - }; - }, - async save(data) { - await models.Settings.edit([{ - key: 'members_stripe_webhook_id', - value: data.webhook_id - }, { - key: 'members_stripe_webhook_secret', - value: data.secret - }]); - } - } -}); - -module.exports.init = async function init() { - configureApi(); - events.on('settings.edited', function (model) { - if (['stripe_publishable_key', 'stripe_secret_key', 'stripe_connect_publishable_key', 'stripe_connect_secret_key'].includes(model.get('key'))) { - debouncedConfigureApi(); - } - }); -}; +module.exports = require('./service'); diff --git a/core/server/services/stripe/service.js b/core/server/services/stripe/service.js new file mode 100644 index 0000000000..f6498dc262 --- /dev/null +++ b/core/server/services/stripe/service.js @@ -0,0 +1,58 @@ +const _ = require('lodash'); +const StripeService = require('@tryghost/members-stripe-service'); +const logging = require('@tryghost/logging'); +const membersService = require('../members'); +const config = require('../../../shared/config'); +const settings = require('../../../shared/settings-cache'); +const urlUtils = require('../../../shared/url-utils'); +const events = require('../../lib/common/events'); +const models = require('../../models'); +const {getConfig} = require('./config'); + +async function configureApi() { + const cfg = getConfig(settings, config, urlUtils); + if (cfg) { + await module.exports.configure(cfg); + return true; + } + return false; +} + +const debouncedConfigureApi = _.debounce(() => { + configureApi(); +}, 600); + +module.exports = new StripeService({ + membersService, + models: _.pick(models, ['Product', 'StripePrice', 'StripeCustomerSubscription', 'StripeProduct', 'MemberStripeCustomer', 'Offer', 'Settings']), + StripeWebhook: { + async get() { + return { + webhook_id: settings.get('members_stripe_webhook_id'), + secret: settings.get('members_stripe_webhook_secret') + }; + }, + async save(data) { + await models.Settings.edit([{ + key: 'members_stripe_webhook_id', + value: data.webhook_id + }, { + key: 'members_stripe_webhook_secret', + value: data.secret + }]); + } + } +}); + +module.exports.init = async function init() { + try { + await configureApi(); + } catch (err) { + logging.error(err); + } + events.on('settings.edited', function (model) { + if (['stripe_publishable_key', 'stripe_secret_key', 'stripe_connect_publishable_key', 'stripe_connect_secret_key'].includes(model.get('key'))) { + debouncedConfigureApi(); + } + }); +};