mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 05:50:35 +03:00
Fixed invalid Offers from throwing when reading
closes https://github.com/TryGhost/Product/issues/3675
refs c98bf80248
As part of our architecture guidelines Repository implementations should protect
against invalid or malformed data in persistence. We do not want read operations
of Entities to throw because of such data. For some fields that bad data can be
fixed or handled in the constructor or static create factory method and replaced
with valid data, others will cause the factory to throw.
This means that Repositories should catch these errors and exclude those
entities from their results. We log the errors in Sentry so that we have
visibility on the state of bad data in DBs
This commit is contained in:
parent
84e6026408
commit
5e3470a3a1
@ -2,6 +2,8 @@ const {flowRight} = require('lodash');
|
|||||||
const {mapKeyValues, mapQuery} = require('@tryghost/mongo-utils');
|
const {mapKeyValues, mapQuery} = require('@tryghost/mongo-utils');
|
||||||
const DomainEvents = require('@tryghost/domain-events');
|
const DomainEvents = require('@tryghost/domain-events');
|
||||||
const {Offer} = require('@tryghost/members-offers');
|
const {Offer} = require('@tryghost/members-offers');
|
||||||
|
const sentry = require('../../../shared/sentry');
|
||||||
|
const logger = require('@tryghost/logging');
|
||||||
|
|
||||||
const statusTransformer = mapKeyValues({
|
const statusTransformer = mapKeyValues({
|
||||||
key: {
|
key: {
|
||||||
@ -99,7 +101,8 @@ class OfferBookshelfRepository {
|
|||||||
const count = await this.OfferRedemptionModel.where({offer_id: json.id}).count('id', {
|
const count = await this.OfferRedemptionModel.where({offer_id: json.id}).count('id', {
|
||||||
transacting: options.transacting
|
transacting: options.transacting
|
||||||
});
|
});
|
||||||
return Offer.create({
|
try {
|
||||||
|
return await Offer.create({
|
||||||
id: json.id,
|
id: json.id,
|
||||||
name: json.name,
|
name: json.name,
|
||||||
code: json.code,
|
code: json.code,
|
||||||
@ -118,6 +121,11 @@ class OfferBookshelfRepository {
|
|||||||
name: json.product.name
|
name: json.product.name
|
||||||
}
|
}
|
||||||
}, null);
|
}, null);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error(err);
|
||||||
|
sentry.captureException(err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,7 +181,7 @@ class OfferBookshelfRepository {
|
|||||||
|
|
||||||
const offers = models.map(model => this.mapToOffer(model, mapOptions));
|
const offers = models.map(model => this.mapToOffer(model, mapOptions));
|
||||||
|
|
||||||
return Promise.all(offers);
|
return (await Promise.all(offers)).filter(offer => offer !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user