1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +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) {
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) {

View File

@ -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,