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