diff --git a/core/server/models/base/plugins/crud.js b/core/server/models/base/plugins/crud.js index faa6944d62..d8fba3729e 100644 --- a/core/server/models/base/plugins/crud.js +++ b/core/server/models/base/plugins/crud.js @@ -1,6 +1,12 @@ const _ = require('lodash'); const errors = require('@tryghost/errors'); +const tpl = require('@tryghost/tpl'); + +const messages = { + couldNotUnderstandRequest: 'Could not understand request.' +}; + /** * @param {Bookshelf} Bookshelf */ @@ -131,7 +137,18 @@ module.exports = function (Bookshelf) { options.columns = _.intersection(options.columns, this.prototype.permittedAttributes()); } - return model.fetch(options); + return model.fetch(options) + .catch((err) => { + // CASE: SQL syntax is incorrect + if (err.errno === 1054 || err.errno === 1) { + throw new errors.BadRequestError({ + message: tpl(messages.couldNotUnderstandRequest), + err + }); + } + + throw err; + }); }, /** diff --git a/test/regression/api/canary/admin/posts_spec.js b/test/regression/api/canary/admin/posts_spec.js index 732f6d0ffd..d6bceb5e27 100644 --- a/test/regression/api/canary/admin/posts_spec.js +++ b/test/regression/api/canary/admin/posts_spec.js @@ -283,6 +283,14 @@ describe('Posts API (canary)', function () { done(); }); }); + + it('throws a 400 when a non-existing field is requested', async function () { + await request.get(localUtils.API.getApiQuery(`posts/slug/${testUtils.DataGenerator.Content.posts[0].slug}/?fields=tags`)) + .set('Origin', config.get('url')) + .expect('Content-Type', /json/) + .expect('Cache-Control', testUtils.cacheRules.private) + .expect(400); + }); }); describe('Add', function () { diff --git a/test/unit/models/base/crud_spec.js b/test/unit/models/base/crud_spec.js index 2a946409be..9faec2073d 100644 --- a/test/unit/models/base/crud_spec.js +++ b/test/unit/models/base/crud_spec.js @@ -89,8 +89,6 @@ describe('Models: crud', function () { const findOneReturnValue = models.Base.Model.findOne(data, unfilteredOptions); - should.equal(findOneReturnValue, fetchStub.returnValues[0]); - return findOneReturnValue.then((result) => { should.equal(result, fetchedModel);