1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00

Fix getFeature() semantics

It will always reject if the feature is not found, not resolve to null.
This commit is contained in:
Jari Bakken 2014-11-14 16:58:05 +01:00
parent d591cdad2b
commit 92eda2660e
2 changed files with 18 additions and 17 deletions

View File

@ -11,13 +11,11 @@ module.exports = function (app) {
}); });
app.get('/features/:featureName', function (req, res) { app.get('/features/:featureName', function (req, res) {
featureDb.getFeature(req.params.featureName).then(function (feature) { featureDb.getFeature(req.params.featureName)
if (feature) { .then(function (feature) { res.json(feature); })
res.json(feature); .catch(function () {
} else {
res.status(404).json({error: 'Could not find feature'}); res.status(404).json({error: 'Could not find feature'});
} });
});
}); });
app.post('/features', function (req, res) { app.post('/features', function (req, res) {
@ -34,7 +32,11 @@ module.exports = function (app) {
var newFeature = req.body; var newFeature = req.body;
var handleFeatureExist = function() { 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 () { 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) { app.put('/features/:featureName', function (req, res) {

View File

@ -31,20 +31,17 @@ function getFeatures() {
function getFeature(name) { function getFeature(name) {
return knex return knex
.select(FEATURE_COLUMNS) .first(FEATURE_COLUMNS)
.from('features') .from('features')
.where({name: name}) .where({name: name})
.limit(1) .then(rowToFeature);
.then(function (rows) {
if (rows.length) {
return rowToFeature(rows[0]);
} else {
throw new Error('could not find feature named: ' + name);
}
});
} }
function rowToFeature(row) { function rowToFeature(row) {
if (!row) {
throw new Error('invalid row');
}
return { return {
name: row.name, name: row.name,
description: row.description, description: row.description,