我正在使用 hapi.js 创建我的第一个 node.js REST Web 服务。我很好奇处理错误的最佳方法,比如说我的 dao 层。我是否throw
将它们放在我的 dao 层中,然后只是try/catch
阻止处理它们并在我的控制器中发回错误,或者有没有更好的方法让酷孩子处理这个问题?
路线/task.js
var taskController = require('../controllers/task');
//var taskValidate = require('../validate/task');
module.exports = function() {
return [
{
method: 'POST',
path: '/tasks/{id}',
config : {
handler: taskController.createTask//,
//validate : taskValidate.blah
}
}
]
}();
控制器/task.js
var taskDao = require('../dao/task');
module.exports = function() {
return {
/**
* Creates a task
*
* @param req
* @param reply
*/
createTask: function createTask(req, reply) {
taskDao.createTask(req.payload, function (err, data) {
// TODO: Properly handle errors in hapi
if (err) {
console.log(err);
}
reply(data);
});
}
}();
道/task.js
module.exports = function() {
return {
createTask: function createTask(payload, callback) {
... Something here which creates the err variable...
if (err) {
console.log(err); // How to properly handle this bad boy
}
}
}();