Nodejs:如何从中间件捕获异常? [英] Nodejs: How to catch an exception from middleware?

查看:164
本文介绍了Nodejs:如何从中间件捕获异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js和less编译器中间件:

I'm using node.js and the "less" compiler middleware:

app.configure(function() {
    // ...
    app.use(express.compiler({ src: __dirname + '/public', enable: ['less'] }))
    // ...
})

现在我有一个错误的文件,但我找不到任何有关如何获取错误信息的文档。
我收到的页面是这样的:

Now i've got a faulty .less-file, but I can't find any docs on how to get the error message. The page I receive is this:

<html>
  <head>
    <title>[object Object]</title>
    <style>
      /* css stuff */
    </style>
  </head>
  <body>
    <div id="wrapper">
      <h1>Connect</h1>

      <h2><em>500</em> [object Object]</h2>
      <ul id="stacktrace"></ul>
    </div>
  </body>
</html>

所以没有帮助。任何人都有想法?

So that's not helpful. Anybody got an idea?

推荐答案

啊,好的,得到了诀窍是离开开发错误Handler

Ah, ok, got it. The trick is to leave away the development errorHandler

app.configure('development', function() {
    // app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

似乎吞下了对app.error的调用,所以现在这样做:

It seems to swallow calls to app.error, so now this works:

app.error(function(err, req, res, next) {
    sys.puts("APP.ERROR:" + sys.inspect(err));
    next(err);
});

这显示正确的错误,而不是[object Object]

This shows the correct error instead of [object Object]

这篇关于Nodejs:如何从中间件捕获异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆