Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript User Authentication With Express and Mongo Improving the App with Custom Middleware Next Steps

Lucas Santos
Lucas Santos
19,315 Points

Wanted to clear up something in this course that I did not fully understand.

One thing I did not fully understand was the error handling structure that is used through out this course.

var err = new Error('You must be logged in to view this page.');
    err.status = 401;
    return next(err);

Create a new error object, give it a status then pass it along as an argument to the next method of the middleware. Ok I get that, but now there are only 2 things that I was confused about in that process.

  1. For what purpose did you pass the error object into the next method?

  2. How is the error being pushed to the front end of error.pug?

extends layout

block content
  .main.container
    .row.text-xs-center
      .col-md-6.col-md-offset-3.p-t-2
        i.icn-person.material-icons error
        p.lead.m-t-2= message 

I see the message variable there but I am not sure how that ties in with there errors we create. Also I know that variables being passed into pug have the hashtag and 2 parentheses like so #{name} which the message variable does not. Other then that those 2 things are the only part of this course I did not completely understand, everything else was a breeze thanks Dave McFarland !

1 Answer

Joel Kraft
STAFF
Joel Kraft
Treehouse Guest Teacher

Hi Lucas,

In Expressjs middleware, calling the next() function passes control to the next middleware. However, if next() is called with an object parameter (i.e. next(err)), control skips all remaining middleware, and will trigger the first error handler to run.

How does Express know the difference between middleware and error handlers? Middleware has 3 arguments in the function signature

function(req, res, next) { /* ... */ }

and error handlers have 4

function(err, req, res, next) { /* ... */ }

So in app.js, the error handler is down toward the bottom, and it looks like this:

// error handler
// define as the last app.use callback
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});

This is where the error lands, and you can see the handler is rendering 'error', which is error.pug.

Here is a video that covers this topic, albeit with JSON instead of renderable templates like error.pug.

Also, here is the documentation if you'd like to read more about it.

Lucas Santos
Lucas Santos
19,315 Points

Got it, thanks appreciate it!