Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npx express-generator does not follow error recommendations #254

Open
sdavids opened this issue Mar 10, 2020 · 3 comments
Open

npx express-generator does not follow error recommendations #254

sdavids opened this issue Mar 10, 2020 · 3 comments

Comments

@sdavids
Copy link

sdavids commented Mar 10, 2020

https://expressjs.com/en/guide/error-handling.html#the-default-error-handler

So when you add a custom error handler, you must delegate to the default Express error handler, when the headers have already been sent to the client

function errorHandler (err, req, res, next) {
  if (res.headersSent) {
    return next(err)
  }
  res.status(500)
  res.render('error', { error: err })
}

The express-generator does not follow the recommendation:

$ npx express-generator
$ grep -A 10 '// error handler' app.js
// 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');
});
@dougwilson dougwilson transferred this issue from expressjs/express Mar 10, 2020
@dougwilson
Copy link
Contributor

I think this may be a documentation issue on the website; there is no requirement to delegate to the default handler if the headers have already been sent; users can do whatever logic they would like to do.

@barraponto
Copy link

from the docs:

If you call next() with an error after you have started writing the response (for example, if you encounter an error while streaming the response to the client) the Express default error handler closes the connection and fails the request.

@oviecodes
Copy link

@sdavids Did you create the error handler after all the other routes. That could cause some issues if you didn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants