Ensured correct usage of @tryghost/errors everywhere

refs: 23b383bedf

- @tryghost/error constructors take an object, not a string - the expectation is that message, context & help should all be set
- This does the bare minimum and just ensures message is set correctly
This commit is contained in:
Hannah Wolfe 2022-02-15 12:27:22 +00:00
parent fbdbd7fd43
commit 3dcf85d5e4
14 changed files with 46 additions and 40 deletions

View File

@ -43,6 +43,7 @@ module.exports = class JWTTokenProvider {
});
if (!claims || typeof claims === 'string') {
// @TODO: throw a detailed error message here
throw new UnauthorizedError();
}

View File

@ -31,7 +31,7 @@ class MagicLink {
*/
constructor(options) {
if (!options || !options.transporter || !options.tokenProvider || !options.getSigninURL) {
throw new IncorrectUsageError('Missing options. Expects {transporter, tokenProvider, getSigninURL}');
throw new IncorrectUsageError({message: 'Missing options. Expects {transporter, tokenProvider, getSigninURL}'});
}
this.transporter = options.transporter;
this.tokenProvider = options.tokenProvider;

View File

@ -88,7 +88,7 @@ class AnalyticEvent {
} else if (typeof data.memberId === 'string') {
memberId = new ObjectID(data.memberId);
} else {
throw new errors.IncorrectUsageError(tpl(messages.missingMemberId));
throw new errors.IncorrectUsageError({mesage: tpl(messages.missingMemberId)});
}
let entryId;
@ -102,19 +102,19 @@ class AnalyticEvent {
const name = data.name;
if (typeof name !== 'string') {
throw new errors.IncorrectUsageError(tpl(messages.invalidEventName));
throw new errors.IncorrectUsageError({message: tpl(messages.invalidEventName)});
}
const timestamp = data.timestamp || new Date();
const sourceUrl = data.sourceUrl;
if (!sourceUrl) {
throw new errors.IncorrectUsageError(tpl(messages.missingSourceUrl));
throw new errors.IncorrectUsageError({message: tpl(messages.missingSourceUrl)});
}
const memberStatus = data.memberStatus;
if (memberStatus !== 'free' && memberStatus !== 'paid' && memberStatus !== 'comped') {
throw new errors.IncorrectUsageError(tpl(messages.invalidMemberStatus));
throw new errors.IncorrectUsageError({message: tpl(messages.invalidMemberStatus)});
}
const metadata = data.metadata || null;

View File

@ -120,14 +120,14 @@ module.exports = class MemberRepository {
const memberData = _.pick(data, ['email', 'name', 'note', 'subscribed', 'geolocation', 'created_at', 'products']);
if (memberData.products && memberData.products.length > 1) {
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
throw new errors.BadRequestError({message: tpl(messages.moreThanOneProduct)});
}
if (memberData.products) {
for (const productData of memberData.products) {
const product = await this._productRepository.get(productData);
if (product.get('active') !== true) {
throw new errors.BadRequestError(tpl(messages.tierArchived));
throw new errors.BadRequestError({message: tpl(messages.tierArchived)});
}
}
}
@ -223,7 +223,7 @@ module.exports = class MemberRepository {
const incomingProductIds = data.products.map(product => product.id);
if (incomingProductIds.length > 1 && incomingProductIds.length > existingProductIds.length) {
throw new errors.BadRequestError(tpl(messages.moreThanOneProduct));
throw new errors.BadRequestError({message: tpl(messages.moreThanOneProduct)});
}
productsToAdd = _.differenceWith(incomingProductIds, existingProductIds);
@ -237,7 +237,7 @@ module.exports = class MemberRepository {
});
if (existingActiveSubscriptions.length) {
throw new errors.BadRequestError(tpl(messages.existingSubscriptions));
throw new errors.BadRequestError({message: tpl(messages.existingSubscriptions)});
}
}
@ -257,7 +257,7 @@ module.exports = class MemberRepository {
for (const productId of productsToAdd) {
const product = await this._productRepository.get({id: productId}, sharedOptions);
if (product.get('active') !== true) {
throw new errors.BadRequestError(tpl(messages.tierArchived));
throw new errors.BadRequestError({message: tpl(messages.tierArchived)});
}
}
@ -512,7 +512,7 @@ module.exports = class MemberRepository {
async linkStripeCustomer(data, options) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'link Stripe Customer'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'link Stripe Customer'})});
}
const customer = await this._stripeAPIService.getCustomer(data.customer_id);
@ -546,7 +546,7 @@ module.exports = class MemberRepository {
async linkSubscription(data, options = {}) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'link Stripe Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'link Stripe Subscription'})});
}
if (!options.transacting) {
@ -569,7 +569,7 @@ module.exports = class MemberRepository {
if (!customer) {
// Maybe just link the customer?
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound)});
}
const subscription = await this._stripeAPIService.getSubscription(data.subscription.id);
@ -802,7 +802,7 @@ module.exports = class MemberRepository {
async getSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'get Stripe Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'get Stripe Subscription'})});
}
const member = await this._Member.findOne({
@ -816,7 +816,7 @@ module.exports = class MemberRepository {
}).fetchOne(options);
if (!subscription) {
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id}));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id})});
}
return subscription.toJSON();
@ -824,7 +824,7 @@ module.exports = class MemberRepository {
async cancelSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'update Stripe Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'update Stripe Subscription'})});
}
let findQuery = null;
@ -835,7 +835,7 @@ module.exports = class MemberRepository {
}
if (!findQuery) {
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound)});
}
const member = await this._Member.findOne(findQuery);
@ -847,7 +847,7 @@ module.exports = class MemberRepository {
}).fetchOne(options);
if (!subscription) {
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id}));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id})});
}
const updatedSubscription = await this._stripeAPIService.cancelSubscription(data.subscription.subscription_id);
@ -860,7 +860,7 @@ module.exports = class MemberRepository {
async updateSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'update Stripe Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'update Stripe Subscription'})});
}
let findQuery = null;
@ -871,7 +871,7 @@ module.exports = class MemberRepository {
}
if (!findQuery) {
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound)});
}
const member = await this._Member.findOne(findQuery);
@ -883,7 +883,7 @@ module.exports = class MemberRepository {
}).fetchOne(options);
if (!subscriptionModel) {
throw new errors.NotFoundError(tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id}));
throw new errors.NotFoundError({message: tpl(messages.subscriptionNotFound, {id: data.subscription.subscription_id})});
}
let updatedSubscription;
@ -927,7 +927,7 @@ module.exports = class MemberRepository {
async createSubscription(data, options) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'create Stripe Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'create Stripe Subscription'})});
}
const member = await this._Member.findOne({
id: data.id
@ -978,7 +978,7 @@ module.exports = class MemberRepository {
}
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'create Complimentary Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'create Complimentary Subscription'})});
}
const member = await this._Member.findOne({
id: data.id
@ -1000,7 +1000,7 @@ module.exports = class MemberRepository {
const defaultProduct = productPage && productPage.data && productPage.data[0] && productPage.data[0].toJSON();
if (!defaultProduct) {
throw new errors.NotFoundError(tpl(messages.productNotFound));
throw new errors.NotFoundError({message: tpl(messages.productNotFound)});
}
const zeroValuePrices = defaultProduct.stripePrices.filter((price) => {
@ -1098,7 +1098,7 @@ module.exports = class MemberRepository {
async cancelComplimentarySubscription(data) {
if (!this._stripeAPIService.configured) {
throw new errors.BadRequestError(tpl(messages.noStripeConnection, {action: 'cancel Complimentary Subscription'}));
throw new errors.BadRequestError({message: tpl(messages.noStripeConnection, {action: 'cancel Complimentary Subscription'})});
}
const member = await this._Member.findOne({

View File

@ -125,7 +125,7 @@ class ProductRepository {
return await this._Product.findOne({slug: data.slug}, options);
}
throw new NotFoundError('Missing id, slug, stripe_product_id or stripe_price_id from data');
throw new NotFoundError({message: 'Missing id, slug, stripe_product_id or stripe_price_id from data'});
}
/**
@ -596,7 +596,7 @@ class ProductRepository {
}
async destroy() {
throw new MethodNotAllowedError('Cannot destroy products, yet...');
throw new MethodNotAllowedError({message: 'Cannot destroy products, yet...'});
}
}

View File

@ -83,7 +83,7 @@ module.exports = class MembersCSVImporter {
const pathExists = await fs.pathExists(outputFilePath);
if (pathExists) {
throw new errors.DataImportError(tpl(messages.filenameCollision));
throw new errors.DataImportError({message: tpl(messages.filenameCollision)});
}
const rows = await membersCSV.parse(inputFilePath, headerMapping, defaultLabels);
@ -115,7 +115,7 @@ module.exports = class MembersCSVImporter {
const job = await this.getJob(id);
if (job.status === 'complete') {
throw new errors.BadRequestError(tpl(messages.jobAlreadyComplete));
throw new errors.BadRequestError({message: tpl(messages.jobAlreadyComplete)});
}
const rows = membersCSV.parse(job.filename);

View File

@ -52,7 +52,7 @@ const server = require('http').createServer(async (req, res) => {
server.listen(0, '127.0.0.1', () => {
const addressInfo = server.address();
if (addressInfo === null || typeof addressInfo === 'string') {
throw new TypeError(`Unexpected return value from server.address(): ${addressInfo}`);
throw new TypeError(`Unexpected return value from server.address(): ${addressInfo}`); /* eslint-disable-line no-restricted-syntax */
}
const {address, port} = addressInfo;
const url = `http://${address}:${port}`;

View File

@ -53,13 +53,13 @@ class MembersSSR {
} = options;
if (!getMembersApi) {
throw new IncorrectUsageError('Missing option getMembersApi');
throw new IncorrectUsageError({message: 'Missing option getMembersApi'});
}
this._getMembersApi = getMembersApi;
if (!cookieKeys) {
throw new IncorrectUsageError('Missing option cookieKeys');
throw new IncorrectUsageError({message: 'Missing option cookieKeys'});
}
this.sessionCookieName = cookieName;
@ -288,7 +288,9 @@ class MembersSSR {
*/
module.exports = function create(options) {
if (!options) {
throw new IncorrectUsageError('Must pass options');
throw new IncorrectUsageError({
message: 'Must pass options'
});
}
return new MembersSSR(options);
};

View File

@ -1,3 +1,6 @@
/* eslint-disable max-lines */
// @TODO: Reduce file length and remove the line above
const DomainEvents = require('@tryghost/domain-events');
const OfferCodeChangeEvent = require('./lib/domain/events/OfferCodeChange');
const OfferCreatedEvent = require('./lib/domain/events/OfferCreated');

View File

@ -20,7 +20,7 @@ class OfferName extends ValueObject {
return new OfferName(name.trim());
}
static InvalidOfferName = InvalidOfferName
static InvalidOfferName = InvalidOfferName;
}
module.exports = OfferName;

View File

@ -19,7 +19,7 @@ class OfferStatus extends ValueObject {
return new OfferStatus(status);
}
static InvalidOfferStatus = InvalidOfferStatus
static InvalidOfferStatus = InvalidOfferStatus;
}
module.exports = OfferStatus;

View File

@ -19,11 +19,11 @@ class OfferType extends ValueObject {
return new OfferType(type);
}
static InvalidOfferType = InvalidOfferType
static InvalidOfferType = InvalidOfferType;
static Percentage = new OfferType('percent')
static Percentage = new OfferType('percent');
static Fixed = new OfferType('fixed')
static Fixed = new OfferType('fixed');
}
module.exports = OfferType;

View File

@ -5,7 +5,7 @@ const {isEqual} = require('lodash');
*/
class ValueObject {
/** @type {{value: T}} */
props
props;
/** @type T */
get value() {

View File

@ -303,7 +303,7 @@ module.exports = class StripeAPI {
enabled_events: events
});
if (webhook.api_version !== STRIPE_API_VERSION) {
throw new VersionMismatchError('Webhook has incorrect api_version');
throw new VersionMismatchError({message: 'Webhook has incorrect api_version'});
}
debug(`updateWebhook(${id}, ${url}) -> Success`);
return webhook;