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

Is this still required? #4

Open
voxelbustersold opened this issue Jul 22, 2019 · 2 comments
Open

Is this still required? #4

voxelbustersold opened this issue Jul 22, 2019 · 2 comments

Comments

@voxelbustersold
Copy link

I see this seems to be handled default by express now. I see there is a try catch in the express code where if any error is thrown, it catches and call next(err) on it.

Layer.prototype.handle_request = function handle(req, res, next) {
  var fn = this.handle;

  if (fn.length > 3) {
    // not a standard request handler
    return next();
  }

  try {
    fn(req, res, next);
  } catch (err) {
    next(err);
  }
};

The above code is from "/functions/node_modules/express/lib/router/layer.js:95:5"

Can you please confirm?

@nataliethistime
Copy link

@voxelbusters I'm having an issue where the above code would seem to indicate that this is not required, however, errors thrown in my routes are not being caught as you would expect. I'll update you here if I find anything interesting...

@nataliethistime
Copy link

@voxelbusters I've found the answer for you.

If you use async/await then express doesn't catch the promise rejecting.

app.get('/test1', (req, res) => {
  throw new Error('Express catches this error');
});

app.get('/test2', async (req, res) => { // <--- note the `async`
  throw new Error('Express does NOT catch this error :(');
});

So yes, you still need this.

Do note however, if you transpile your Express code using a tool like Babel, I suspect that express would catch those errors normally. I'm not sure why you would transpile server code though 🤷‍♂

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