gpt4 book ai didi

node.js - 在 Express 的自定义错误处理程序中获取堆栈跟踪?

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:51 24 4
gpt4 key购买 nike

我在 Express 中使用自定义错误页面,如解释的那样 here .

但是当我这样做时,我只看到错误消息。我想获取与默认 Express 错误处理程序(堆栈跟踪等)中显示的相同信息,以便我可以:

  1. 将其记录到控制台(如果我可以为此保留默认值,我会很高兴)。
  2. 在错误页面上显示它,但仅限于本地主机。

我该怎么做?

最佳答案

这是@generalhenry 回答的修改版本。您可以访问堆栈跟踪在 err.stack 中,这样您就可以将它传递给您的“500” View 并在其上做一些花哨的 css 样式。

app.use(function(err, req, res, next) {
if (err instanceof NotFound) {
res.render('errors/404');
} else {
res.render('errors/500', {error: err, stack: err.stack});
}
});

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

// below all route handlers
// If all fails, hit em with the 404
app.all('*', function(req, res){
throw new NotFound;
});

关于node.js - 在 Express 的自定义错误处理程序中获取堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com