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

"UnhandledPromiseRejectionWarning" if middleware throws exception (Version 1.0.0) #31

Open
nschmit opened this issue Mar 3, 2022 · 1 comment

Comments

@nschmit
Copy link

nschmit commented Mar 3, 2022

When my authentication middleware is throwing an exception (see code below), an 'UnhandledPromiseRejectionWarning" is issued. The exception is not propagated further, which causes problems in my program.
This problem did not exist in the previous version 0.5.0.

export function requiresAuth(req, res, next) {
  if (req.session && req.session.user && req.session.user.id) {
    return next();
  } else {
    throw new NoValidSessionError('requiresAuth');
  }
}
requiresAuth.unless = require('express-unless');

Possible fix in index.js:

    try {
      middleware(req, res, next);
    } catch(err) {next(err)}
@nathanlepori
Copy link

This has become a bigger issue in newer node versions since now unhandled rejections crash the process. Also note that Express v5 will handle promise rejections and forward to error handlers so it might be worth to do that in this middleware as well. The fix should be pretty easy I think

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

No branches or pull requests

2 participants