From d1cd0fe80e57ccf88bf27a64404128c0bba688e8 Mon Sep 17 00:00:00 2001 From: Fabien 'egg' O'Carroll Date: Tue, 21 Jul 2020 13:40:49 +0200 Subject: [PATCH] Caught & handled 'resource_already_exists' errors (#185) refs https://github.com/TryGhost/Ghost/issues/12065 This protects us against multiple instances of the members-api being started simultaneously and race conditions where inbetween the initial "GET" of a plan which returns empty, and the "POST" of a plan to create it, another instance has already created it. --- ghost/members-api/lib/stripe/api/createDeterministicApi.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ghost/members-api/lib/stripe/api/createDeterministicApi.js b/ghost/members-api/lib/stripe/api/createDeterministicApi.js index 1e53e59611..0dadaff096 100644 --- a/ghost/members-api/lib/stripe/api/createDeterministicApi.js +++ b/ghost/members-api/lib/stripe/api/createDeterministicApi.js @@ -43,7 +43,12 @@ function createCreator(resource, getAttrs) { stripe, resource, Object.assign(getAttrs(object, ...rest), {id}) - ); + ).catch((err) => { + if (err.code !== 'resource_already_exists') { + throw err; + } + return stripeRetrieve(stripe, resource, id); + }); }; }