This commit is contained in:
Christopher Van 2015-01-20 15:36:37 -08:00
Родитель 08d5d183d3
Коммит e704c7d4bc
2 изменённых файлов: 32 добавлений и 32 удалений

Просмотреть файл

@ -26,29 +26,8 @@ var gameSchema = {
};
// This wraps the handlers and calls the following for each the promise
// returned by the controller handlers:
//
// …
// .catch(function (err) {
//
// reply(utils.returnError(err));
// });
//
var safeHandler = function (func) {
return function (request, reply) {
func.apply(this, arguments).catch(function (err) {
reply(utils.returnError(err));
});
};
};
exports.all = {
handler: safeHandler(function (request, reply) {
handler: utils.safeHandler(function (request, reply) {
return Game.objects.all(request.params)
.then(function (games) {
@ -63,7 +42,7 @@ exports.create = {
validate: {
payload: gameSchema
},
handler: safeHandler(function (request, reply) {
handler: utils.safeHandler(function (request, reply) {
return Game.objects.create(request.payload)
.then(function (res) {
@ -77,7 +56,7 @@ exports.create = {
exports.get = {
handler: safeHandler(function (request, reply) {
handler: utils.safeHandler(function (request, reply) {
return Game.objects.get(request.params)
.then(function (game) {
@ -89,7 +68,7 @@ exports.get = {
exports.remove = {
handler: safeHandler(function (request, reply) {
handler: utils.safeHandler(function (request, reply) {
return Game.objects.remove(request.params)
.then(reply);
@ -101,7 +80,7 @@ exports.update = {
validate: {
payload: gameSchema
},
handler: safeHandler(function (request, reply) {
handler: utils.safeHandler(function (request, reply) {
return Game.objects.update(request.params, request.payload)
.then(function (res) {

Просмотреть файл

@ -3,21 +3,21 @@
var Boom = require('boom');
module.exports.errors = require('./errors');
exports.errors = require('./errors');
module.exports.isStringAnInt = function (str) {
exports.isStringAnInt = function (str) {
var num = Number(str);
return String(num) === str && num >= 0;
};
var stringifyObject = module.exports.stringifyObject = function (obj) {
exports.stringifyObject = function (obj) {
return typeof obj === 'object' ? JSON.stringify(obj) : obj;
};
module.exports.promisify = function (func) {
exports.promisify = function (func) {
return function () {
// Bail if the function is already a promise.
if (func && typeof func.then === 'function') {
@ -39,7 +39,7 @@ module.exports.promisify = function (func) {
};
module.exports.returnError = function (err) {
exports.returnError = function (err) {
if (err instanceof Object) {
if (err instanceof Error) {
if (process.env.NODE_ENV !== 'test') {
@ -47,7 +47,7 @@ module.exports.returnError = function (err) {
}
} else if (err.name) {
if (process.env.NODE_ENV !== 'test') {
console.error('Caught rejection:\n%s', stringifyObject(err));
console.error('Caught rejection:\n%s', exports.stringifyObject(err));
}
switch (err.name) {
@ -65,3 +65,24 @@ module.exports.returnError = function (err) {
return Boom.badImplementation(err);
};
// This wraps the handlers and calls the following for each the promise
// returned by the controller handlers:
//
// …
// .catch(function (err) {
//
// reply(utils.returnError(err));
// });
//
exports.safeHandler = function (func) {
return function (request, reply) {
func.apply(this, arguments).catch(function (err) {
reply(exports.returnError(err));
});
};
};