Scroll back to the top

Express framework: Common error handler

[ javascript express nodejs ]

The Express framework doesn’t allow us to set up a global error handler before assign route handlers. It’s not handy, because we do all common settings before all concrete application stuff, usually.

There is a workaround in the case of the express framework. Create and assign express.Router on the root URL, then register a global error handler.

const router = express.Router();
httpServer.use('/', router);
httpServer.use(errorHandler);
function errorHandler(err, req, res, next) {
  _logger.error(err.stack);
  if (res.headersSent) {
    return next(err)
  }
  if (req.xhr || req.headers.accept.indexOf('json') > -1) {
    res.status(500).send({ error: err.message || 'Server error' })
  } else {
    res.status(500).send(err.message || 'Server Error')
  }
}

All following route handlers should be registered within the Router object instead of the main express instance. Now it’s ok to register route handlers after a common error handler.

 router.get('/some/url', async (req, res, next) => {
  try {
  
    // ...

    next();
  } catch (err) {
    next(err);
  }
});