use boom to surface hapi API errors

This commit is contained in:
Christopher Van 2014-12-17 16:10:47 -08:00
Родитель 1b167f9fb6
Коммит fabac447c2
5 изменённых файлов: 50 добавлений и 37 удалений

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

@ -26,33 +26,9 @@ module.exports = {
remove: function () { remove: function () {
}, },
get: function (request) { get: function (request) {
return new Promise(function (resolve, reject) { return Game.objects.get(request.pg.client, {
var game = new Game(request.params); idOrSlug: request.params.idOrSlug
}).catch(utils.returnError);
var query = (utils.isNumeric(request.params.idOrSlug) ?
'SELECT * FROM games WHERE id = $1' :
'SELECT * FROM games WHERE slug = $1'
);
request.pg.client.query(query, [request.params.idOrSlug],
function (err, result) {
if (err) {
return reject(err);
}
if (!result.rows.length) {
// TODO: Return proper JSON when row couldn't be seleced.
return reject({
statusCode: 404,
error: 'Not Found',
message: 'No game found'
});
}
// TODO: Throw error for `err` when row couldn't get be selected.
resolve(err || result.rows[0]);
});
});
}, },
update: function () { update: function () {
} }

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

@ -1,5 +1,7 @@
var Promise = require('es6-promise').Promise; var Promise = require('es6-promise').Promise;
var utils = require('../../lib/utils');
function Game(data) { function Game(data) {
// Game Description is optional. // Game Description is optional.
@ -17,11 +19,27 @@ function Game(data) {
this.slug = data.slug; this.slug = data.slug;
} }
Game.get = function get(data) { Game.objects = {};
Game.objects.get = function (db, data) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var query = (utils.isNumeric(data.idOrSlug) ?
'SELECT * FROM games WHERE id = $1' :
'SELECT * FROM games WHERE slug = $1'
);
db.query(query, [data.idOrSlug], function (err, result) {
if (err) {
return reject(err);
}
if (!result.rows.length) {
return reject(utils.errors.DoesNotExist());
}
// TODO: Throw error when row couldn't get be selected (#259).
resolve(err || result.rows[0]);
});
}); });
}; };

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

@ -59,13 +59,7 @@ module.exports = function (server) {
method: 'GET', method: 'GET',
path: '/games/{idOrSlug}', path: '/games/{idOrSlug}',
handler: function (request, reply) { handler: function (request, reply) {
console.log('games/id handler'); gameController.get(request).then(reply).catch(reply);
gameController.get(request).then(reply, function (response) {
reply(response).code(response.statusCode);
}).catch(function (err) {
console.error(err);
reply(err);
});
}, },
// config: { // config: {
// validate: { // validate: {

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

@ -1,12 +1,21 @@
var Boom = require('boom');
var Promise = require('es6-promise').Promise; var Promise = require('es6-promise').Promise;
module.exports.errors = require('./errors');
module.exports.isNumeric = function isNumeric(obj) { module.exports.isNumeric = function isNumeric(obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj); return !isNaN(parseFloat(obj)) && isFinite(obj);
}; };
module.exports.promisify = function promisify(func) { module.exports.stringifyObject = stringifyObject = function (obj) {
return obj instanceof Object ? JSON.stringify(obj) : obj;
};
module.exports.promisify = function (func) {
return function () { return function () {
var args = Array.prototype.slice.apply(arguments); var args = Array.prototype.slice.apply(arguments);
@ -21,3 +30,18 @@ module.exports.promisify = function promisify(func) {
}); });
}; };
}; };
module.exports.returnError = function (err) {
console.error('Caught error: %s', stringifyObject(err));
switch (err.name) {
case 'DatabaseError':
return Boom.badImplementation(err.message || 'database_error');
case 'DoesNotExist':
return Boom.notFound(err.message || 'does_not_exist');
case 'ValidationError':
return Boom.badRequest(err.message || 'validation_error');
default:
return Boom.badImplementation();
}
};

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

@ -7,7 +7,8 @@
"joi": "^5.0.2", "joi": "^5.0.2",
"pg": "^4.1.1", "pg": "^4.1.1",
"hapi-node-postgres": "cvan/hapi-node-postgres#no-pg-native", "hapi-node-postgres": "cvan/hapi-node-postgres#no-pg-native",
"node-pg-migrate": "0.0.7" "node-pg-migrate": "0.0.7",
"boom": "^2.6.1"
}, },
"devDependencies": { "devDependencies": {
"gulp": "^3.8.7", "gulp": "^3.8.7",