javascript hapi.js 处理错误的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24682656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hapi.js best way to handle errors
提问by Catfish
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?
我正在使用 hapi.js 创建我的第一个 node.js REST Web 服务。我很好奇处理错误的最佳方法让我们从我的 dao 层说起。我是throw
在我的 dao 层try/catch
中处理它们,然后只是块来处理它们并在我的控制器中发回错误,还是有更好的方法让酷孩子们处理这个问题?
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
道/任务.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
}
}
}();
采纳答案by Catfish
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
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);
});
}
}();
dao/task.js
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);
}
}();
回答by nelsonic
Generic Solution w/ Fully CustomisableError Template/Messages
具有完全可定制的错误模板/消息的通用解决方案
We wrote a Hapi Plugin that handles all errors seamlessly: npmjs.com/package/hapi-error
我们写了一个哈皮插件,无缝地处理所有错误:npmjs.com/package/高致病性禽流感发生错误
It lets you define your own custom error pages in 3 easy steps.
它使您可以通过 3 个简单的步骤定义自己的自定义错误页面。
1. Install the pluginfrom npm:
1.从 npm安装插件:
npm install hapi-error --save
2. Include the plugin in your Hapi project
2. 在你的 Hapi 项目中包含插件
Include the plugin when you register
your server:
在您register
的服务器时包含插件:
server.register([require('hapi-error'), require('vision')], function (err) {
// your server code here ...
});
See: /example/server_example.jsfor simple example
请参阅:/example/server_example.js获取简单示例
3. Ensure that you have a View called error_template
3.确保您有一个名为的视图 error_template
Note:
hapi-error
plugin expectsyou are usingVision
(the standard view rendering library for Hapi apps) which allows you to use Handlebars, Jade, React, etc. for your templates.
注:
hapi-error
插件希望您使用Vision
(用于哈皮标准视图渲染库应用程式),它允许您使用的手把,翡翠,作出反应,等等。为您的模板。
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 HapistatusCode
- *HTTP statusCode sent to the client e.g:404
(not found)errorMessage
- the human-friendly error message
errorTitle
- Hapi 生成的错误图块statusCode
- * HTTP的StatusCode发送到客户端如:404
(未找到)errorMessage
-人性化的错误信息
for an example see:
/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 应用程序可以处理所有类型的错误,您也可以抛出自己的自定义错误!
Note:
hapi-error
works for REST/APIs too. if the content type header (headers.acceps
) is set toapplication/json
then your app will return a JSON error to the client, otherwise an HTML page will be served.
注意:也
hapi-error
适用于 REST/API。如果内容类型标头 (headers.acceps
) 设置为,application/json
则您的应用程序将向客户端返回 JSON 错误,否则将提供 HTML 页面。
回答by Ricardo Barros
I think the cool kids now use a package to caught unhandled errors with Hapi, I present to you, Poop.
我认为很酷的孩子现在使用一个包来捕获 Hapi 未处理的错误,我向你展示,Poop。
The only thing Poop is missing is some rich documentation, but check it out, and you'll see that Poop is great.
Poop 唯一缺少的是一些丰富的文档,但请查看一下,您会发现 Poop 很棒。
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.
我的一些朋友去了里斯本的 node.js 活动,主持人是 Wallmart 负责网络技术堆栈的人,他们使用 Hapi.js、Poop 和其他一些很酷的东西。
So if they use poop it must be pretty awesome.
因此,如果他们使用便便,那一定非常棒。
PS: The name is suppa awesome
PS:名字真棒