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

Support Async Middleware #495

Open
xjamundx opened this issue Sep 20, 2017 · 6 comments
Open

Support Async Middleware #495

xjamundx opened this issue Sep 20, 2017 · 6 comments

Comments

@xjamundx
Copy link
Contributor

xjamundx commented Sep 20, 2017

I'd love to see us patch express/kraken to directly provide support for async functions.

Specifically I want the errors from an async middleware function to automatically get forwarded into the express default error handler.

Here's an example:

async function getUsers(req, res, next) {
  let users = await getUsersForId(req.id)
  res.json(users)
}

server.get('/users', getUsers) // <-- failures in getUsers() cause a crash

Instead we end up wrapping our async middleware with a function like this:

// Takes an async middleware function. Returns middleware with error handling added.
let handleError = p => (req, res, next) => p(req, req, next).catch(next)

server.use('/users', handleError(getUsers))

But sometimes we forget them and things still blow up.

We address this with Flow, but we don't need to, we can actually add this directly into the platform with something like this.

let oldGet = server.get.bind(server)
server.get = (path, ...routes) => {
  oldGet(path, routes.map(x => {
    if (x.catch) {
      return (req, res, next) => x(req, res, next).catch(next)
    }
    return x
  }))
}
@dougwilson
Copy link

Perhaps Express can add a hook to register a function to wrap middleware, which could be used to automatically wrap the middleware / handers for this and whatever other purposes. The details for how the interaction will be done with routers will need to be hammered out, but a high-level idea is:

app.wrap(function (fn) {
  return function (req, res, next) {
    fn(req, res, next).catch(next)
  }
})

This is very high-level thought, but figured I'd start a discussion here from the comment in Express repo to get a feeling of if this could be a solution or not.

@xjamundx
Copy link
Contributor Author

xjamundx commented Sep 20, 2017

@dougwilson I think at this stage with async/await finally built-in to the platform, it should just become a core express/kraken feature to support promise-based middleware (without even having to opt-in via app.wrap).

It sounds like express 5 is stalling pretty hard (which is fine), so our best bet will be to add this support directly into kraken I think.

@dougwilson
Copy link

Yea, and it should, but there are still unresolved issues on how exactly we should deal with the resolved values, for example, and how to deal with the Bluebird warnings, as another.

@xjamundx
Copy link
Contributor Author

On the kraken side there a few ways to implement this. One place we may want to look at is in express-enrouten such as here:
https://github.com/krakenjs/express-enrouten/blob/v1.x/lib/directory.js#L69

@xjamundx
Copy link
Contributor Author

Another thing we could do is put this monkey-patching script into its own module and just require it in as needed (either in kraken core or as wanted by various teams):
https://gist.github.com/q42jaap/f2fb93d96fda6384d3e3fc51977dec90

xjamundx added a commit to xjamundx/express-enrouten that referenced this issue Sep 22, 2017
This goes along with krakenjs/kraken-js#495 and is a proof-of-concept at this point, not really production ready. There are a few potential ways to support promise-based middleware. This is only one of them.
xjamundx added a commit to xjamundx/express-enrouten that referenced this issue Sep 22, 2017
This goes along with krakenjs/kraken-js#495 and is a proof-of-concept at this point, not really production ready. There are a few potential ways to support promise-based middleware. This is only one of them.
@xjamundx
Copy link
Contributor Author

We could potentially integrate with this module:
https://github.com/davidbanham/express-async-errors

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