如何正确处理 Express 中的错误?

发布于 2024-12-09 05:44:40 字数 1371 浏览 0 评论 0原文

我开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声静候 2024-12-16 05:44:40

您需要查看快速错误处理。从那里:

app.param('userId', function(req, res, next, id) {
    User.get(id, function(err, user) {
        if (err) return next(err);
        if (!user) return next(new Error('failed to find user'));
        req.user = user;
        next();
    });
});

您缺少的最佳点是 return next(...)

You'll want to check out Express Error Handling. From there:

app.param('userId', function(req, res, next, id) {
    User.get(id, function(err, user) {
        if (err) return next(err);
        if (!user) return next(new Error('failed to find user'));
        req.user = user;
        next();
    });
});

The sweetspot that you are missing is the return next(...)

黑白记忆 2024-12-16 05:44:40

那是因为你做错了:你已经抛出了一个错误(该错误将由 Express 处理并为用户返回 500 - 错误页面或类似的内容),但你也尝试将你自己的响应发送给客户端: res.send('发现事件!');

您确实应该在此处查看有关错误处理的 Express 指南: http://expressjs.com/guide /error-handling.html

在你的例子中我会做的是:

function NotFound(msg){
  this.name = 'NotFound';
  Error.call(this, msg);
  Error.captureStackTrace(this, arguments.callee);
} 

app.get('/event/:id', function(req, res, next){
  if (req.params.id != 1) {
    throw new NotFound('Cannot find event ' + req.params.id);
  } else {
    res.send('event found!');
  }
});

app.error(function(err, req, res, next){
    if (err instanceof NotFound) {
        res.render('404.ejs');
    } else {
        next(err);
    }
});

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:

function NotFound(msg){
  this.name = 'NotFound';
  Error.call(this, msg);
  Error.captureStackTrace(this, arguments.callee);
} 

app.get('/event/:id', function(req, res, next){
  if (req.params.id != 1) {
    throw new NotFound('Cannot find event ' + req.params.id);
  } else {
    res.send('event found!');
  }
});

app.error(function(err, req, res, next){
    if (err instanceof NotFound) {
        res.render('404.ejs');
    } else {
        next(err);
    }
});
芸娘子的小脾气 2024-12-16 05:44:40

您的代码中存在一些问题:

  • 响应客户端时,您需要使用响应对象res 而不是比 req)。

  • 当向 next 发送错误时,您应该返回,这样函数的其余部分就不会运行。

这是修复这些错误后的代码:

app.get('/event/:id', function(req, res, next) {
    if (req.params.id != 1) {
        return next(new Error('cannot find event ' + req.params.id));
    }

    res.send('event found!'); // use res.send (NOT req.send)
}); 

You have a couple of problems in your code:

  • When responding to the client, you need to use the response object (res rather than req).

  • 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:

app.get('/event/:id', function(req, res, next) {
    if (req.params.id != 1) {
        return next(new Error('cannot find event ' + req.params.id));
    }

    res.send('event found!'); // use res.send (NOT req.send)
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文