Express: error handler doesn't work if I remove next parameter

Created on 13 Aug 2018  路  6Comments  路  Source: expressjs/express

I'm learning Express.js and using the generator I generated a new app with:

npm install express-generator -g && express myapp

After that I saw in app.js that there is this code:

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      next(createError(404));
    });

    // error handler
    app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get("env") === "development" ? err : {};

      // render the error page
      res.status(err.status || 500);
      res.render("error");
    });

enter image description here

As you can see in the image eslint is complaining about next parameter, declared and never used. I agree, but if I remove it express doesn't render the error page.

Why this behavior?

This is dangerous for me because I can't trust anymore eslint or my coding degree? I'm surely missing something. But what?

Most helpful comment

This is correct behavior. You may need to configure your eslint to work better with Express.js code. We recommend StandardJS or one of it's derritives.

You can read more about how error handling works in Express and why the next argument is important here: http://expressjs.com/en/guide/error-handling.html

All 6 comments

This is correct behavior. You may need to configure your eslint to work better with Express.js code. We recommend StandardJS or one of it's derritives.

You can read more about how error handling works in Express and why the next argument is important here: http://expressjs.com/en/guide/error-handling.html

@frederikhors you can add this line right above the line with the next argument - // eslint-disable-next-line no-unused-vars

@niftylettuce thanks. I dropped out eslint for StandardJS.

@niftylettuce but still I think something is not elegant in this way...

Yeah, I agree on it not being super elegant, and would personally like to see it changed in 5.x, there is an open issue about this here: #2896

Also I have a proposed fix for this in this comment: https://github.com/pillarjs/router/pull/59#issuecomment-383409639

If anyone has feedback I would appreciate it. I am willing to put together the final PR for that if people are happy with the approach, but I have been waiting on feedback to that post.

Was this page helpful?
0 / 5 - 0 ratings