reduced some duplication in handling error responses

This commit is contained in:
tigerbot 2017-10-17 16:16:57 -06:00
parent f2ce3e9fe1
commit 9c7aaa4f98
1 changed files with 40 additions and 47 deletions

View File

@ -46,6 +46,16 @@ module.exports.create = function (deps, conf) {
}; };
} }
function handlePromise(req, res, prom) {
prom.then(function (result) {
res.send(deps.recase.snakeCopy(result));
}).catch(function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
});
}
function isAuthorized(req, res, fn) { function isAuthorized(req, res, fn) {
var auth = jwt.decode((req.headers.authorization||'').replace(/^bearer\s+/i, '')); var auth = jwt.decode((req.headers.authorization||'').replace(/^bearer\s+/i, ''));
if (!auth) { if (!auth) {
@ -369,7 +379,7 @@ module.exports.create = function (deps, conf) {
return; return;
} }
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var update; var update;
if (req.params.group) { if (req.params.group) {
update = {}; update = {};
@ -381,16 +391,13 @@ module.exports.create = function (deps, conf) {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
changer.update(update); changer.update(update);
return config.save(changer); return config.save(changer);
}).then(function (config) { }).then(function (newConf) {
if (req.params.group) { if (req.params.group) {
config = config[req.params.group]; return newConf[req.params.group];
} }
res.send(deps.recase.snakeCopy(config)); return newConf;
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.extractModList = function (changer, params) { config.extractModList = function (changer, params) {
@ -424,7 +431,7 @@ module.exports.create = function (deps, conf) {
return; return;
} }
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params); var modList = config.extractModList(changer, req.params);
@ -436,12 +443,9 @@ module.exports.create = function (deps, conf) {
return config.save(changer); return config.save(changer);
}).then(function (newConf) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params))); return config.extractModList(newConf, req.params);
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.restful.updateModule = function (req, res, next) { config.restful.updateModule = function (req, res, next) {
if (req.params.group === 'domains') { if (req.params.group === 'domains') {
@ -449,18 +453,17 @@ module.exports.create = function (deps, conf) {
return; return;
} }
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params); var modList = config.extractModList(changer, req.params);
modList.update(req.params.modId, req.body); modList.update(req.params.modId, req.body);
return config.save(changer); return config.save(changer);
}).then(function (newConf) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params))); return config.extractModule(newConf, req.params).find(function (mod) {
}, function (err) { return mod.id === req.params.modId;
res.statusCode = err.statusCode || 500; });
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.restful.removeModule = function (req, res, next) { config.restful.removeModule = function (req, res, next) {
if (req.params.group === 'domains') { if (req.params.group === 'domains') {
@ -468,22 +471,19 @@ module.exports.create = function (deps, conf) {
return; return;
} }
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params); var modList = config.extractModList(changer, req.params);
modList.remove(req.params.modId); modList.remove(req.params.modId);
return config.save(changer); return config.save(changer);
}).then(function (newConf) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params))); return config.extractModList(newConf, req.params);
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.restful.createDomain = function (req, res) { config.restful.createDomain = function (req, res) {
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
var update = req.body; var update = req.body;
@ -492,16 +492,13 @@ module.exports.create = function (deps, conf) {
} }
update.forEach(changer.domains.add, changer.domains); update.forEach(changer.domains.add, changer.domains);
return config.save(changer); return config.save(changer);
}).then(function (config) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.domains)); return newConf.domains;
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.restful.updateDomain = function (req, res) { config.restful.updateDomain = function (req, res) {
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
if (req.body.modules) { if (req.body.modules) {
throw Object.assign(new Error('do not add modules with this route'), {statusCode: 400}); throw Object.assign(new Error('do not add modules with this route'), {statusCode: 400});
} }
@ -509,26 +506,22 @@ module.exports.create = function (deps, conf) {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
changer.domains.update(req.params.domId, req.body); changer.domains.update(req.params.domId, req.body);
return config.save(changer); return config.save(changer);
}).then(function (config) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.domains)); return newConf.domains.find(function (dom) {
}, function (err) { return dom.id === req.params.domId;
res.statusCode = err.statusCode || 500; });
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
config.restful.removeDomain = function (req, res) { config.restful.removeDomain = function (req, res) {
deps.PromiseA.resolve().then(function () { var promise = deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
changer.domains.remove(req.params.domId); changer.domains.remove(req.params.domId);
return config.save(changer); return config.save(changer);
}).then(function (config) { }).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.domains)); return newConf.domains;
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
handlePromise(req, res, promise);
}; };
var app = require('express')(); var app = require('express')();