哈皮神。js处理错误的最佳方式

时间:2022-08-12 20:09:49

I'm creating my first node.js REST web service using hapi.js. I'm curious as to the best way to handle errors let's say from my dao layer. Do i throw them in my dao layer and then just try/catch blocks to handle them and send back errors in my controller, or is there a better way that the cool kids are handling this?

我在创建第一个节点。js REST web服务使用hapi.js。我很想知道处理错误的最佳方式,比如说从我的dao层。我是把它们放到我的dao层中,然后尝试/捕获块来处理它们并在我的控制器中发送错误,还是有更好的方式让那些酷孩子们处理这些?

routes/task.js

路线/ 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
      }
    }
  ]
}();

controllers/task.js

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

    }
}();

dao/task.js

刀/ 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
    }
  }
}();

3 个解决方案

#1


6  

In doing more research along with Ricardo Barros' comment on using Boom, here's what I ended up with.

里卡多·巴罗斯(Ricardo Barros)关于使用Boom的评论,在做更多研究时,我得出了以下结论。

controllers/task.js

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

    }
}();

dao/task.js

刀/ 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);
  }
}();

#2


4  

Generic Solution w/ Fully Customisable Error Template/Messages

We wrote a Hapi Plugin that handles all errors seamlessly: npmjs.com/package/hapi-error

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

哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式

It lets you define your own custom error pages in 3 easy steps.

它允许您用三个简单的步骤定义您自己的自定义错误页面。

1. Install the plugin from npm:

npm install hapi-error --save

2. Include the plugin in your Hapi project

Include the plugin when you register your server:

当你注册你的服务器时包括插件:

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

See: /example/server_example.js for simple example

看到:/ / server_example示例。js的简单的例子

3. Ensure that you have a View called error_template

Note: hapi-error plugin expects you are using Vision (the standard view rendering library for Hapi apps) which allows you to use Handlebars, Jade, React, etc. for your templates.

注意:Hapi -error插件希望您使用Vision (Hapi应用程序的标准视图呈现库),它允许您使用Handlebars、Jade、React等模板。

Your error_template.html (or error_template.ext error_template.jsx) should make use of the 3 variables it will be passed:

你的error_template。html(或error_template。ext error_template.jsx)应利用它将要传递的3个变量:

  • errorTitle - the error tile generated by Hapi
  • errorTitle——Hapi生成的错误块
  • statusCode - *HTTP statusCode sent to the client e.g: 404 (not found)
  • *HTTP状态码发送到客户端e。旅客:404(未找到)
  • errorMessage - the human-friendly error message
  • 错误消息——人类友好的错误消息。

for an example see: /example/error_template.html

例如:/example/error_template.html

That's it! Now your Hapi App handles all types of errors and you can throw your own custom ones too!

就是这样!现在,您的Hapi应用程序处理所有类型的错误,您也可以抛出您自己的自定义错误!

哈皮神。js处理错误的最佳方式

Note: hapi-error works for REST/APIs too. if the content type header (headers.acceps) is set to application/json then your app will return a JSON error to the client, otherwise an HTML page will be served.

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

#3


1  

I think the cool kids now use a package to caught unhandled errors with Hapi, I present to you, Poop.

我想现在的酷孩子们用Hapi来捕捉未处理的错误,我给你们看,便便。

The only thing Poop is missing is some rich documentation, but check it out, and you'll see that Poop is great.

唯一缺少的是一些丰富的文献资料,但是看看它,你会发现便便很好。

Some of my friends went to a node.js event in Lisbon, on of the hosts was a guy in charge of web technology stack at Wallmart, they use Hapi.js, Poop and some other cool things.

我的一些朋友去了一个节点。在里斯本举行的js event上,on的主持人是Wallmart网站上负责web技术栈的一个人,他们使用Hapi。js,便便和其他一些很酷的东西。

So if they use poop it must be pretty awesome.

所以如果他们使用便便,那一定非常棒。

PS: The name is suppa awesome

PS:名字是suppa awesome

#1


6  

In doing more research along with Ricardo Barros' comment on using Boom, here's what I ended up with.

里卡多·巴罗斯(Ricardo Barros)关于使用Boom的评论,在做更多研究时,我得出了以下结论。

controllers/task.js

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

    }
}();

dao/task.js

刀/ 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);
  }
}();

#2


4  

Generic Solution w/ Fully Customisable Error Template/Messages

We wrote a Hapi Plugin that handles all errors seamlessly: npmjs.com/package/hapi-error

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

哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式 哈皮神。js处理错误的最佳方式

It lets you define your own custom error pages in 3 easy steps.

它允许您用三个简单的步骤定义您自己的自定义错误页面。

1. Install the plugin from npm:

npm install hapi-error --save

2. Include the plugin in your Hapi project

Include the plugin when you register your server:

当你注册你的服务器时包括插件:

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

See: /example/server_example.js for simple example

看到:/ / server_example示例。js的简单的例子

3. Ensure that you have a View called error_template

Note: hapi-error plugin expects you are using Vision (the standard view rendering library for Hapi apps) which allows you to use Handlebars, Jade, React, etc. for your templates.

注意:Hapi -error插件希望您使用Vision (Hapi应用程序的标准视图呈现库),它允许您使用Handlebars、Jade、React等模板。

Your error_template.html (or error_template.ext error_template.jsx) should make use of the 3 variables it will be passed:

你的error_template。html(或error_template。ext error_template.jsx)应利用它将要传递的3个变量:

  • errorTitle - the error tile generated by Hapi
  • errorTitle——Hapi生成的错误块
  • statusCode - *HTTP statusCode sent to the client e.g: 404 (not found)
  • *HTTP状态码发送到客户端e。旅客:404(未找到)
  • errorMessage - the human-friendly error message
  • 错误消息——人类友好的错误消息。

for an example see: /example/error_template.html

例如:/example/error_template.html

That's it! Now your Hapi App handles all types of errors and you can throw your own custom ones too!

就是这样!现在,您的Hapi应用程序处理所有类型的错误,您也可以抛出您自己的自定义错误!

哈皮神。js处理错误的最佳方式

Note: hapi-error works for REST/APIs too. if the content type header (headers.acceps) is set to application/json then your app will return a JSON error to the client, otherwise an HTML page will be served.

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

#3


1  

I think the cool kids now use a package to caught unhandled errors with Hapi, I present to you, Poop.

我想现在的酷孩子们用Hapi来捕捉未处理的错误,我给你们看,便便。

The only thing Poop is missing is some rich documentation, but check it out, and you'll see that Poop is great.

唯一缺少的是一些丰富的文献资料,但是看看它,你会发现便便很好。

Some of my friends went to a node.js event in Lisbon, on of the hosts was a guy in charge of web technology stack at Wallmart, they use Hapi.js, Poop and some other cool things.

我的一些朋友去了一个节点。在里斯本举行的js event上,on的主持人是Wallmart网站上负责web技术栈的一个人,他们使用Hapi。js,便便和其他一些很酷的东西。

So if they use poop it must be pretty awesome.

所以如果他们使用便便,那一定非常棒。

PS: The name is suppa awesome

PS:名字是suppa awesome