Catch and trace middleware exceptions (less-middleware) in Express 3.0

First of all, I tried to follow this question (but adapted to Express 3.0):

Nodejs: How to catch an exception from middleware?

app.use(function(err, req, res, next) {
    console.error('Middleware exception:' + err.stack);
    next(err);
});

However, no matter where I put it in my middleware chain, before or after app.use(lessMiddleware); (from require('less-middleware')) I still get:

GET /? 304 44ms
  source : <my path>/style.less
  dest : <my path>/style.css
  read : <my path>/style.less
DEBUG: 

DEBUG: <my path>/node_modules/less-middleware/node_modules/less/lib/less/parser.js:385
                        throw new(LessError)(e, env);
                              ^

DEBUG: [object Object]

DEBUG: Program node app.js exited with code 1

Kind readers, how to solve this problem? I want to:

  • Show errror stacktrace
  • Show server error page instead of crashing

To my surprise the problem was in less-middleware. I kind of expected it to be a more mature solution. I fixed it and made a pull request on Github. Hope this helps anybody encountering the same problem. Cheers.