error-handler.js 692 B

123456789101112131415161718192021222324
  1. const _dispatcher = require('./dispatcher');
  2. module.exports = async function(router, options={}){
  3. // create new disptacher with given options
  4. const dispatch = await _dispatcher(options);
  5. // default 404 error - no route matches
  6. router.all('*', function(req, res){
  7. dispatch('404', 404, req, res);
  8. });
  9. // internal errors (all)
  10. router.use(function(err, req, res, next){
  11. // status code given ?
  12. if (err.status){
  13. // custom errorpage set ?
  14. dispatch(err.errorpage || err.status, err.status, req, res);
  15. // use default http500 page
  16. }else{
  17. dispatch('500', 500, req, res);
  18. }
  19. });
  20. };