luin / express-promise

❤️ Middleware for easy rendering of async Query results.
MIT License
316 stars 19 forks source link

Error-handling middleware on mounted router not called #18

Open nemtsov opened 8 years ago

nemtsov commented 8 years ago

The router-specific error handler should be called first on errors within a router prior to hitting the global error-handler. This doesn't happen when an error is handled from within the promisified res.json().

Repro:

import express, {Router} from 'express';

async function bad() {
  throw new Error('bad');
}

const router = new Router();

router.get('/:name', (req, res) => {
  res.json(bad());
});

// error handler on this router is never called
router.use((err, req, res, next) => {
  res.status(400);
});

const app = express();
app.use('/', router);