mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-28 00:17:12 +01:00
Cleaned up and changed promises used in the feature API
This commit is contained in:
parent
c2a809a819
commit
ae0fdce395
13
lib/error/NameExistsError.js
Normal file
13
lib/error/NameExistsError.js
Normal file
@ -0,0 +1,13 @@
|
||||
var util = require('util');
|
||||
|
||||
function NameExistsError(message) {
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
util.inherits(NameExistsError, Error);
|
||||
|
||||
module.exports = NameExistsError;
|
13
lib/error/NotFoundError.js
Normal file
13
lib/error/NotFoundError.js
Normal file
@ -0,0 +1,13 @@
|
||||
var util = require('util');
|
||||
|
||||
function NotFoundError(message) {
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
util.inherits(NotFoundError, Error);
|
||||
|
||||
module.exports = NotFoundError;
|
13
lib/error/ValidationError.js
Normal file
13
lib/error/ValidationError.js
Normal file
@ -0,0 +1,13 @@
|
||||
var util = require('util');
|
||||
|
||||
function ValidationError(message) {
|
||||
Error.call(this);
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
|
||||
this.name = this.constructor.name;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
util.inherits(ValidationError, Error);
|
||||
|
||||
module.exports = ValidationError;
|
14
lib/error/requestValidator.js
Normal file
14
lib/error/requestValidator.js
Normal file
@ -0,0 +1,14 @@
|
||||
var Promise = require("bluebird");
|
||||
var ValidationError = require('./ValidationError');
|
||||
|
||||
function validateRequest(req) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
if (req.validationErrors()) {
|
||||
reject(new ValidationError("Invalid syntax"));
|
||||
} else {
|
||||
resolve(req);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = validateRequest;
|
@ -1,6 +1,11 @@
|
||||
var logger = require('./logger');
|
||||
var eventStore = require('./eventStore');
|
||||
var eventType = require('./eventType');
|
||||
var featureDb = require('./featureDb');
|
||||
var NameExistsError = require('./error/NameExistsError');
|
||||
var NotFoundError = require('./error/NotFoundError');
|
||||
var ValidationError = require('./error/ValidationError');
|
||||
var requestValidator = require('./error/requestValidator');
|
||||
|
||||
module.exports = function (app) {
|
||||
|
||||
@ -22,62 +27,55 @@ module.exports = function (app) {
|
||||
req.checkBody('name', 'Name is required').notEmpty();
|
||||
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
|
||||
|
||||
var errors = req.validationErrors();
|
||||
|
||||
if (errors) {
|
||||
res.status(400).json(errors);
|
||||
return;
|
||||
}
|
||||
|
||||
var newFeature = req.body;
|
||||
|
||||
var handleFeatureExist = function() {
|
||||
var errors = [
|
||||
{msg: "A feature named '" + newFeature.name + "' already exists."}
|
||||
];
|
||||
|
||||
res.status(403).json(errors).end();
|
||||
};
|
||||
|
||||
var handleCreateFeature = function () {
|
||||
eventStore.create({
|
||||
type: eventType.featureCreated,
|
||||
createdBy: req.connection.remoteAddress,
|
||||
data: newFeature
|
||||
requestValidator(req)
|
||||
.then(featureDb.validateUniqueName)
|
||||
.then(function() {
|
||||
return eventStore.create({
|
||||
type: eventType.featureCreated,
|
||||
createdBy: req.connection.remoteAddress,
|
||||
data: req.body
|
||||
});
|
||||
})
|
||||
.then(function () { res.status(201).end(); })
|
||||
.catch(function () { res.status(500).end(); });
|
||||
};
|
||||
|
||||
featureDb.getFeature(newFeature.name)
|
||||
.then(handleFeatureExist)
|
||||
.catch(handleCreateFeature);
|
||||
});
|
||||
.then(function () {
|
||||
res.status(201).end();
|
||||
})
|
||||
.catch(NameExistsError, function() {
|
||||
res.status(403).json([{msg: "A feature named '" + req.body.name + "' already exists."}]).end();
|
||||
})
|
||||
.catch(ValidationError, function() {
|
||||
res.status(400).json(req.validationErrors());
|
||||
})
|
||||
.catch(function(err) {
|
||||
logger.error("Could not create feature toggle", err);
|
||||
res.status(500).end();
|
||||
});
|
||||
});
|
||||
|
||||
app.put('/features/:featureName', function (req, res) {
|
||||
var featureName = req.params.featureName;
|
||||
var createdBy = req.connection.remoteAddress;
|
||||
var userName = req.connection.remoteAddress;
|
||||
var updatedFeature = req.body;
|
||||
|
||||
updatedFeature.name = featureName;
|
||||
|
||||
var event = {
|
||||
type: eventType.featureUpdated,
|
||||
createdBy: createdBy,
|
||||
data: updatedFeature
|
||||
};
|
||||
|
||||
featureDb.getFeature(featureName)
|
||||
.then(function () {
|
||||
eventStore
|
||||
.create(event)
|
||||
.then(function () { res.status(200).end(); })
|
||||
.catch(function () { res.status(500).end(); });
|
||||
return eventStore.create({
|
||||
type: eventType.featureUpdated,
|
||||
createdBy: userName,
|
||||
data: updatedFeature
|
||||
});
|
||||
})
|
||||
.catch(function () {
|
||||
.then(function () {
|
||||
res.status(200).end();
|
||||
})
|
||||
.catch(NotFoundError, function () {
|
||||
res.status(404).end();
|
||||
})
|
||||
.catch(function (err) {
|
||||
logger.error("Could not update feature toggle="+featureName, err);
|
||||
res.status(500).end();
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
@ -2,6 +2,9 @@ var eventStore = require('./eventStore');
|
||||
var eventType = require('./eventType');
|
||||
var logger = require('./logger');
|
||||
var knex = require('./dbPool');
|
||||
var Promise = require("bluebird");
|
||||
var NotFoundError = require('./error/NotFoundError');
|
||||
var NameExistsError = require('./error/NameExistsError');
|
||||
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
|
||||
|
||||
eventStore.on(eventType.featureCreated, function (event) {
|
||||
@ -39,7 +42,7 @@ function getFeature(name) {
|
||||
|
||||
function rowToFeature(row) {
|
||||
if (!row) {
|
||||
throw new Error('invalid row');
|
||||
throw new NotFoundError('No feature toggle found');
|
||||
}
|
||||
|
||||
return {
|
||||
@ -61,8 +64,20 @@ function eventToRow(event) {
|
||||
};
|
||||
}
|
||||
|
||||
function validateUniqueName(req) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
getFeature(req.body.name)
|
||||
.then(function() {
|
||||
reject(new NameExistsError("Feature name already exist"));
|
||||
}, function() {
|
||||
resolve(req);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getFeatures: getFeatures,
|
||||
getFeature: getFeature
|
||||
getFeature: getFeature,
|
||||
validateUniqueName: validateUniqueName
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
var Promise = require("bluebird");
|
||||
var NotFoundError = require('../lib/error/NotFoundError');
|
||||
|
||||
var features = [
|
||||
{
|
||||
@ -49,7 +50,7 @@ module.exports = {
|
||||
if(feature) {
|
||||
return Promise.resolve(feature);
|
||||
} else {
|
||||
return Promise.reject("feature not found");
|
||||
return Promise.reject(new NotFoundError("feature not found"));
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user