diff --git a/lib/featureApi.js b/lib/featureApi.js index f0165ca149..3f511d423d 100644 --- a/lib/featureApi.js +++ b/lib/featureApi.js @@ -11,13 +11,11 @@ module.exports = function (app) { }); app.get('/features/:featureName', function (req, res) { - featureDb.getFeature(req.params.featureName).then(function (feature) { - if (feature) { - res.json(feature); - } else { + featureDb.getFeature(req.params.featureName) + .then(function (feature) { res.json(feature); }) + .catch(function () { res.status(404).json({error: 'Could not find feature'}); - } - }); + }); }); app.post('/features', function (req, res) { @@ -34,7 +32,11 @@ module.exports = function (app) { var newFeature = req.body; var handleFeatureExist = function() { - res.status(403).end(); + var errors = [ + {msg: "A feature named '" + newFeature.name + "' already exists."} + ]; + + res.status(403).json(errors).end(); }; var handleCreateFeature = function () { @@ -49,7 +51,9 @@ module.exports = function (app) { }); }; - featureDb.getFeature(newFeature.name).then(handleFeatureExist, handleCreateFeature); + featureDb.getFeature(newFeature.name) + .then(handleFeatureExist) + .catch(handleCreateFeature); }); app.put('/features/:featureName', function (req, res) { diff --git a/lib/featureDb.js b/lib/featureDb.js index 0141047ee7..924fb23e41 100644 --- a/lib/featureDb.js +++ b/lib/featureDb.js @@ -31,20 +31,17 @@ function getFeatures() { function getFeature(name) { return knex - .select(FEATURE_COLUMNS) + .first(FEATURE_COLUMNS) .from('features') .where({name: name}) - .limit(1) - .then(function (rows) { - if (rows.length) { - return rowToFeature(rows[0]); - } else { - throw new Error('could not find feature named: ' + name); - } - }); + .then(rowToFeature); } function rowToFeature(row) { + if (!row) { + throw new Error('invalid row'); + } + return { name: row.name, description: row.description,