hapi.js处理错误的最佳方法

Cat*_*ish 12 javascript rest node.js hapijs

我正在使用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
      }
    }
  ]
}();
Run Code Online (Sandbox Code Playgroud)

控制器/ 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);
      });

    }
}();
Run Code Online (Sandbox Code Playgroud)

DAO/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
    }
  }
}();
Run Code Online (Sandbox Code Playgroud)

nel*_*nic 8

具有完全可自定义的错误模板/消息的通用解决方案

我们编写了一个Hapi插件,可以无缝处理所有错误:npmjs.com/package/ hapi-error

建立状态 codecov.io 代码气候 依赖状态

它允许您通过3个简单步骤定义自己的自定义错误页面.

1. 从npm 安装插件:

npm install hapi-error --save
Run Code Online (Sandbox Code Playgroud)

2.在Hapi项目中包含插件

在您register的服务器上包含插件:

server.register([require('hapi-error'), require('vision')], function (err) {
 // your server code here ...
});
Run Code Online (Sandbox Code Playgroud)

有关简单示例,请参阅:/example/server_example.js

3.确保您有一个名为的视图 error_template

注:hapi-error插件希望您使用Vision(渲染哈皮的应用程序库中的标准视图),它允许您使用的手把,翡翠,作出反应,等等.为您的模板.

error_template.html(error_template.ext error_template.jsx)应该使用它将传递的3个变量:

  • errorTitle- Hapi生成的错误瓦片
  • statusCode-*HTTP的StatusCode发送到客户端如:404(未找到)
  • errorMessage- 人性化的错误消息

举个例子看: /example/error_template.html

就是这样!现在您的Hapi App处理所有类型的错误,您也可以自己定制错误!

高致病性禽流感发生误差屏

注意:也hapi-error适用于REST/API.如果内容类型header(headers.acceps)设置为,application/json那么您的应用程序将向客户端返回JSON错误,否则将提供HTML页面.


Cat*_*ish 6

在进行更多研究以及里卡多巴罗斯关于使用Boom的评论时,这就是我最终的结果.

controllers/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) {

        if (err) {
          return reply(Boom.badImplementation(err));
        }

        return reply(data);
      });

    }
}();
Run Code Online (Sandbox Code Playgroud)

dao/task.js

module.exports = function() {

  return {
    createTask: function createTask(payload, callback) {

    //.. Something here which creates the variables err and myData ...

    if (err) {
      return callback(err);
    }

    //... If successful ...
    callback(null, myData);
  }
}();
Run Code Online (Sandbox Code Playgroud)