diff --git a/lib/error/requestValidator.js b/lib/error/validateRequest.js similarity index 100% rename from lib/error/requestValidator.js rename to lib/error/validateRequest.js diff --git a/lib/featureApi.js b/lib/featureApi.js index 69bd86e70a..fe9bd715b8 100644 --- a/lib/featureApi.js +++ b/lib/featureApi.js @@ -6,7 +6,7 @@ var featureDb = require('./featureDb'); var NameExistsError = require('./error/NameExistsError'); var NotFoundError = require('./error/NotFoundError'); var ValidationError = require('./error/ValidationError'); -var requestValidator = require('./error/requestValidator'); +var validateRequest = require('./error/validateRequest'); module.exports = function (app) { @@ -28,7 +28,7 @@ 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); - requestValidator(req) + validateRequest(req) .then(validateUniqueName) .then(function() { return eventStore.create({ diff --git a/lib/strategyApi.js b/lib/strategyApi.js index facc307ebf..6a1e469d5e 100644 --- a/lib/strategyApi.js +++ b/lib/strategyApi.js @@ -1,6 +1,11 @@ -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var strategyDb = require('./strategyDb'); +var Promise = require("bluebird"); +var eventStore = require('./eventStore'); +var eventType = require('./eventType'); +var strategyDb = require('./strategyDb'); +var logger = require('./logger'); +var NameExistsError = require('./error/NameExistsError'); +var ValidationError = require('./error/ValidationError'); +var validateRequest = require('./error/validateRequest'); module.exports = function (app) { @@ -32,34 +37,42 @@ 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 newStrategy = req.body; - var handleStrategyExists = function() { - var errors = [{msg: "A strategy named " + newStrategy.name + " already exists."}]; - res.status(403).json(errors); - }; - - var handleCreateStrategy = function() { - eventStore.create({ - type: eventType.strategyCreated, - createdBy: req.connection.remoteAddress, - data: newStrategy + validateRequest(req) + .then(validateStrategyName) + .then(function() { + return eventStore.create({ + type: eventType.strategyCreated, + createdBy: req.connection.remoteAddress, + data: newStrategy + }); }) - .then(function () { res.status(201).end(); }) - .catch(function () { res.status(500).end(); }); - }; - - strategyDb.getStrategy(newStrategy.name) - .then(handleStrategyExists) - .catch(handleCreateStrategy); + .then(function () { + res.status(201).end(); + }) + .catch(NameExistsError, function() { + res.status(403).json([{msg: "A strategy named '" + req.body.name + "' already exists."}]).end(); + }) + .catch(ValidationError, function() { + res.status(400).json(req.validationErrors()); + }) + .catch(function(err) { + logger.error("Could not create strategy", err); + res.status(500).end(); + }); }); + function validateStrategyName(req) { + return new Promise(function(resolve, reject) { + strategyDb.getStrategy(req.body.name) + .then(function() { + reject(new NameExistsError("Feature name already exist")); + }, function() { + resolve(req); + }); + }); + } + };