Nodejs: How to catch an exception from middleware?

做~自己de王妃 提交于 2019-12-01 04:00:14

问题


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?


回答1:


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 }));
});

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);
});

This shows the correct error instead of [object Object]



来源:https://stackoverflow.com/questions/4063764/nodejs-how-to-catch-an-exception-from-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!