【问题标题】:Where to catch this error?在哪里捕捉到这个错误?
【发布时间】:2012-03-12 07:42:18
【问题描述】:

我正在使用 expressjs

我这样创建应用程序:

app.createServer(
  express.cookieParser(),
  express.bodyParser(),
  myfunc1(),
  myfunc2()
);

在 myfunc1 中,我在某些情况下返回 next(err)。 (例如 req.query 包含一些奇怪的字符)。

所以,如果发生这种情况( next(err) ),它只是简单地返回 http 响应 500。我尝试使用以下方法捕获错误:

app.error(....)  or
process.on("uncaughtexception" ... );

但错误并没有发生在这两个方面。他哪里出错了?

【问题讨论】:

  • app.error(...); 使用的代码是什么?我很确定这是正确的做法。

标签: exception node.js error-handling express


【解决方案1】:

Express 指南中对此进行了很好的介绍:

app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));

这将捕获错误并为您提供错误屏幕(对于大多数例外情况)。该指南详细介绍了如何处理 404、500 等:

http://expressjs.com/guide.html#error-handling

编码愉快!

【讨论】:

  • 我已经这样做了。但是我在 app.get('/500', ...) 和 app.error(...) 上设置了断点,但从未到达那里。很奇怪。
  • 这很奇怪。我在这里有一个使用自定义中间件进行错误处理的示例。这对我有用:github.com/JustinBeckwith/ExpressStarter
猜你喜欢
  • 2011-03-11
  • 2022-08-15
  • 1970-01-01
  • 2014-07-04
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2019-01-20
相关资源
最近更新 更多