0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Added a better error when a user try to create an orphaned webhook

issue https://github.com/TryGhost/Team/issues/526
refs 70627d84a7 (r48575450)
This commit is contained in:
Thibaut Patel 2021-03-23 17:15:21 +01:00
parent 2996180c60
commit 44035fd591
2 changed files with 39 additions and 1 deletions

View file

@ -25,7 +25,20 @@ module.exports = {
throw new errors.ValidationError({message: i18n.t('errors.api.webhooks.webhookAlreadyExists')});
}
return models.Webhook.add(frame.data.webhooks[0], frame.options);
try {
const newWebhook = await models.Webhook.add(frame.data.webhooks[0], frame.options);
return newWebhook;
} catch (error) {
if (error.errno === 1452 || (error.code === 'SQLITE_CONSTRAINT' && /SQLITE_CONSTRAINT: FOREIGN KEY constraint failed/.test(error.message))) {
throw new errors.ValidationError({
message: i18n.t('notices.data.validation.index.schemaValidationFailed', {
key: 'integration_id'
}),
context: i18n.t('errors.api.webhooks.nonExistingIntegrationIdProvided.context'),
help: i18n.t('errors.api.webhooks.nonExistingIntegrationIdProvided.help')
});
}
}
}
},

View file

@ -54,6 +54,31 @@ describe('Webhooks API', function () {
.expect(422);
});
it('Fails nicely when creating an orphaned webhook', async function () {
const webhookData = {
event: 'test.create',
target_url: 'http://example.com/webhooks/test/extra/10',
name: 'test',
secret: 'thisissecret',
api_version: API_VERSION,
integration_id: `fake-integration`
};
const res = await request.post(localUtils.API.getApiQuery('webhooks/'))
.set('Origin', config.get('url'))
.send({webhooks: [webhookData]})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(422);
const jsonResponse = res.body;
should.exist(jsonResponse.errors);
jsonResponse.errors[0].type.should.equal('ValidationError');
jsonResponse.errors[0].context.should.equal(`Validation failed for 'integration_id'. 'integration_id' value does not match any existing integration.`);
});
it('Can edit a webhook', async function () {
let createdIntegration;
let createdWebhook;