Skip to content

Instantly share code, notes, and snippets.

@IanSmith89
Last active June 23, 2016 17:36
Show Gist options
  • Save IanSmith89/b9eaf52b98a831bebbee8014e368b812 to your computer and use it in GitHub Desktop.
Save IanSmith89/b9eaf52b98a831bebbee8014e368b812 to your computer and use it in GitHub Desktop.

For today’s warmup, your goal is to refactor your error handling middleware to render an error template.

  • Create a new template in the views directory called error.ejs
  • Change your 404 and 500 error handlers to render error.ejs with information from the error object. (err.message, err.status, err.stack, etc.)
  • Use Materialize to style the page however you’d like, think of GitHub’s 404 page for inspiration.

Your refactored error handlers could look like this:

app.use(function(_req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

app.use(function(err, _req, res, _next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment