Fixed 500 error when deleting items that don't exist

fixes #11723

- when deleting an invite/label/tag/webhook that doesn't
  exist, Ghost would throw a 500 error
- this commit catches the NotFoundError
- also rejects from model if nothing was found
- spotted in Sentry
This commit is contained in:
Daniel Lockyer 2020-04-13 11:21:47 +01:00
parent 946f7b872f
commit a769bbe86c
9 changed files with 60 additions and 7 deletions

View File

@ -77,7 +77,12 @@ module.exports = {
frame.options.require = true;
return models.Invite.destroy(frame.options)
.then(() => null);
.then(() => null)
.catch(models.Invite.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.invites.inviteNotFound')
}));
});
}
},

View File

@ -139,7 +139,13 @@ module.exports = {
},
permissions: true,
query(frame) {
return models.Label.destroy(frame.options).then(() => null);
return models.Label.destroy(frame.options)
.then(() => null)
.catch(models.Label.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.labels.labelNotFound')
}));
});
}
}
};

View File

@ -142,7 +142,13 @@ module.exports = {
},
permissions: true,
query(frame) {
return models.Tag.destroy(frame.options).then(() => null);
return models.Tag.destroy(frame.options)
.then(() => null)
.catch(models.Tag.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.tags.tagNotFound')
}));
});
}
}
};

View File

@ -84,7 +84,16 @@ module.exports = {
permissions: true,
query(frame) {
frame.options.require = true;
return models.Webhook.destroy(frame.options).then(() => null);
return models.Webhook.destroy(frame.options)
.then(() => null)
.catch(models.Webhook.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.resource.resourceNotFound', {
resource: 'Webhook'
})
}));
});
}
}
};

View File

@ -77,7 +77,12 @@ module.exports = {
frame.options.require = true;
return models.Invite.destroy(frame.options)
.then(() => null);
.then(() => null)
.catch(models.Invite.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.invites.inviteNotFound')
}));
});
}
},

View File

@ -142,7 +142,13 @@ module.exports = {
},
permissions: true,
query(frame) {
return models.Tag.destroy(frame.options).then(() => null);
return models.Tag.destroy(frame.options)
.then(() => null)
.catch(models.Tag.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.tags.tagNotFound')
}));
});
}
}
};

View File

@ -84,7 +84,15 @@ module.exports = {
permissions: true,
query(frame) {
frame.options.require = true;
return models.Webhook.destroy(frame.options).then(() => null);
return models.Webhook.destroy(frame.options)
.then(() => null)
.catch(models.Webhook.NotFoundError, () => {
return Promise.reject(new common.errors.NotFoundError({
message: common.i18n.t('errors.api.resource.resourceNotFound', {
resource: 'Webhook'
})
}));
});
}
}
};

View File

@ -107,6 +107,10 @@ Label = ghostBookshelf.Model.extend({
return this.forge({id: options.id})
.fetch(options)
.then(function destroyLabelsAndMember(label) {
if (!label) {
return Promise.reject();
}
return label.related('members')
.detach(null, options)
.then(function destroyLabels() {

View File

@ -117,6 +117,10 @@ Tag = ghostBookshelf.Model.extend({
return this.forge({id: options.id})
.fetch(options)
.then(function destroyTagsAndPost(tag) {
if (!tag) {
return Promise.reject();
}
return tag.related('posts')
.detach(null, options)
.then(function destroyTags() {