如何正确处理 Express 中的错误?
我开始使用 Express JS 并遇到了一个问题。我似乎无法找出处理错误的正确方法。
例如,我有一个 Web 服务 API,它为一个名为“事件”的对象提供服务。当用户提交未找到的事件 ID 时,我想返回一个简单的“找不到事件”字符串。以下是我目前构建代码的方式:
app.get('/event/:id', function(req, res, next) {
if (req.params.id != 1) {
next(new Error('cannot find event ' + req.params.id));
}
req.send('event found!');
});
当我提交 1 以外的 id 时,Node 崩溃并显示以下输出:
http.js:527
throw new Error("Can't set headers after they are sent.");
^
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/usr/local/kayak/node_modules/express/node_modules/connect/lib/patch.js:62:20)
at /usr/local/kayak/node_modules/express/node_modules/connect/lib/middleware/errorHandler.js:72:19
at [object Object].<anonymous> (fs.js:107:5)
at [object Object].emit (events.js:61:17)
at afterRead (fs.js:878:12)
at wrapper (fs.js:245:17)
据我所知,通过使用 node.js 调试器< /em>,在调用 next()
后继续执行代码块,这意味着 req.send('event found!')
尝试运行。我不希望这种事发生。
我发现的唯一解决方法是简单地抛出一个 new Error()
而不是“next-ing”它,但这会导致生成默认的 Express HTML 错误页面。我想要更多一点的控制权。
我花时间阅读了 Express 文档的错误处理部分,但是我无法理解它。
I am beginning to work with Express JS and have run into an issue. I can't seem to figure out the proper way to handle errors.
For example, I have a web services API that serves an object called "event". I'd like to return a simple string of "cannot find event" when a user submits an event id that isn't found. Here is how I'm currently structuring my code:
app.get('/event/:id', function(req, res, next) {
if (req.params.id != 1) {
next(new Error('cannot find event ' + req.params.id));
}
req.send('event found!');
});
When I submit an id other than 1, Node crashes with the following output:
http.js:527
throw new Error("Can't set headers after they are sent.");
^
Error: Can't set headers after they are sent.
at ServerResponse.<anonymous> (http.js:527:11)
at ServerResponse.setHeader (/usr/local/kayak/node_modules/express/node_modules/connect/lib/patch.js:62:20)
at /usr/local/kayak/node_modules/express/node_modules/connect/lib/middleware/errorHandler.js:72:19
at [object Object].<anonymous> (fs.js:107:5)
at [object Object].emit (events.js:61:17)
at afterRead (fs.js:878:12)
at wrapper (fs.js:245:17)
From what I can tell by using the node.js debugger, execution of the block of code continues after next()
is called, meaning that req.send('event found!')
tries to run. I don't want this to happen.
The only workaround that I've found is to simply throw a new Error()
instead of "next-ing" it, but this results in a default Express HTML error page being generated. I'd like a little more control than that.
I have taken the time to read over the error handling section of the Express documentation, but I couldn't make sense of it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要查看快速错误处理。从那里:
您缺少的最佳点是
return
next(...)
You'll want to check out Express Error Handling. From there:
The sweetspot that you are missing is the
return
next(...)
那是因为你做错了:你已经抛出了一个错误(该错误将由 Express 处理并为用户返回 500 - 错误页面或类似的内容),但你也尝试将你自己的响应发送给客户端: res.send('发现事件!');
您确实应该在此处查看有关错误处理的 Express 指南: http://expressjs.com/guide /error-handling.html
在你的例子中我会做的是:
That's because you're doing it wrong: you already threw an Error (which will be processed by Express and return a 500 - Error page for the user or something like that) but you are also trying to send your own response to the client: res.send('event found!');
You should really check out the Express guide about Error Handling here: http://expressjs.com/guide/error-handling.html
What I would do in your example is:
您的代码中存在一些问题:
响应客户端时,您需要使用响应对象(
res
而不是比req
)。当向
next
发送错误时,您应该返回,这样函数的其余部分就不会运行。这是修复这些错误后的代码:
You have a couple of problems in your code:
When responding to the client, you need to use the response object (
res
rather thanreq
).When sending an error to
next
, you should return, so the rest of the function doesn't run.Here's your code after fixing those errors: