From 3e1084905e5fe74847aee6af7309b82e01ad2f94 Mon Sep 17 00:00:00 2001
From: Fabien O'Carroll <fabien@allou.is>
Date: Wed, 14 Jul 2021 14:11:33 +0100
Subject: [PATCH] Removed usage of raw Error class

refs https://github.com/TryGhost/Team/issues/879
---
 ghost/magic-link/index.js                     | 4 +++-
 ghost/magic-link/lib/JWTTokenProvider.js      | 3 ++-
 ghost/members-api/lib/repositories/product.js | 6 +++---
 ghost/members-api/lib/services/stripe-api.js  | 3 ++-
 ghost/members-ssr/MembersSSR.js               | 9 +++++----
 5 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/ghost/magic-link/index.js b/ghost/magic-link/index.js
index c83359848d..862124b22a 100644
--- a/ghost/magic-link/index.js
+++ b/ghost/magic-link/index.js
@@ -1,3 +1,5 @@
+const {IncorrectUsageError} = require('@tryghost/errors');
+
 /**
  * @typedef { import('nodemailer').Transporter } MailTransporter
  * @typedef { import('nodemailer').SentMessageInfo } SentMessageInfo
@@ -29,7 +31,7 @@ class MagicLink {
      */
     constructor(options) {
         if (!options || !options.transporter || !options.tokenProvider || !options.getSigninURL) {
-            throw new Error('Missing options. Expects {transporter, tokenProvider, getSigninURL}');
+            throw new IncorrectUsageError('Missing options. Expects {transporter, tokenProvider, getSigninURL}');
         }
         this.transporter = options.transporter;
         this.tokenProvider = options.tokenProvider;
diff --git a/ghost/magic-link/lib/JWTTokenProvider.js b/ghost/magic-link/lib/JWTTokenProvider.js
index ef79b3e739..436dc0f489 100644
--- a/ghost/magic-link/lib/JWTTokenProvider.js
+++ b/ghost/magic-link/lib/JWTTokenProvider.js
@@ -1,3 +1,4 @@
+const {UnauthorizedError} = require('@tryghost/errors');
 const jwt = require('jsonwebtoken');
 
 /**
@@ -42,7 +43,7 @@ module.exports = class JWTTokenProvider {
         });
 
         if (!claims || typeof claims === 'string') {
-            throw new Error();
+            throw new UnauthorizedError();
         }
 
         return claims;
diff --git a/ghost/members-api/lib/repositories/product.js b/ghost/members-api/lib/repositories/product.js
index f71a11f312..5ff09be144 100644
--- a/ghost/members-api/lib/repositories/product.js
+++ b/ghost/members-api/lib/repositories/product.js
@@ -1,4 +1,4 @@
-const {UpdateCollisionError} = require('@tryghost/errors');
+const {UpdateCollisionError, NotFoundError, MethodNotAllowedError} = require('@tryghost/errors');
 
 /**
  * @typedef {object} ProductModel
@@ -87,7 +87,7 @@ class ProductRepository {
             return await this._Product.findOne({slug: data.slug}, options);
         }
 
-        throw new Error('Missing id, slug, stripe_product_id or stripe_price_id from data');
+        throw new NotFoundError('Missing id, slug, stripe_product_id or stripe_price_id from data');
     }
 
     /**
@@ -469,7 +469,7 @@ class ProductRepository {
     }
 
     async destroy() {
-        throw new Error('Cannot destroy products, yet...');
+        throw new MethodNotAllowedError('Cannot destroy products, yet...');
     }
 }
 
diff --git a/ghost/members-api/lib/services/stripe-api.js b/ghost/members-api/lib/services/stripe-api.js
index cd9dd58ba9..c9d8b23cfc 100644
--- a/ghost/members-api/lib/services/stripe-api.js
+++ b/ghost/members-api/lib/services/stripe-api.js
@@ -1,3 +1,4 @@
+const {VersionMismatchError} = require('@tryghost/errors');
 const debug = require('@tryghost/debug')('services/stripe');
 const Stripe = require('stripe').Stripe;
 const LeakyBucket = require('leaky-bucket');
@@ -293,7 +294,7 @@ module.exports = class StripeAPIService {
                 enabled_events: events
             });
             if (webhook.api_version !== STRIPE_API_VERSION) {
-                throw new Error('Webhook has incorrect api_version');
+                throw new VersionMismatchError('Webhook has incorrect api_version');
             }
             debug(`updateWebhook(${id}, ${url}) -> Success`);
             return webhook;
diff --git a/ghost/members-ssr/MembersSSR.js b/ghost/members-ssr/MembersSSR.js
index b3e23aa474..d805c2130a 100644
--- a/ghost/members-ssr/MembersSSR.js
+++ b/ghost/members-ssr/MembersSSR.js
@@ -3,7 +3,8 @@ const createCookies = require('cookies');
 const debug = require('@tryghost/debug')('members-ssr');
 
 const {
-    BadRequestError
+    BadRequestError,
+    IncorrectUsageError
 } = require('@tryghost/ignition-errors');
 
 /**
@@ -52,13 +53,13 @@ class MembersSSR {
         } = options;
 
         if (!getMembersApi) {
-            throw new Error('Missing option getMembersApi');
+            throw new IncorrectUsageError('Missing option getMembersApi');
         }
 
         this._getMembersApi = getMembersApi;
 
         if (!cookieKeys) {
-            throw new Error('Missing option cookieKeys');
+            throw new IncorrectUsageError('Missing option cookieKeys');
         }
 
         this.sessionCookieName = cookieName;
@@ -287,7 +288,7 @@ class MembersSSR {
  */
 module.exports = function create(options) {
     if (!options) {
-        throw new Error('Must pass options');
+        throw new IncorrectUsageError('Must pass options');
     }
     return new MembersSSR(options);
 };