Middy: Error handlers not catching properly with async middlewares

Created on 17 Nov 2017  路  3Comments  路  Source: middyjs/middy

An async middleware with async keyword or just a middleware that implements asynchronous work, doesn't seem to properly bubble up the error object for middlewares that implement the onError method.

This doesn't seem to be properly caught:

export default function() {
  return {
    before: async (handler, next) => {
      throw new Error('wow');
    }
  };
}

Or even something like this:

export default function() {
  return {
    before: (handler, next) => {
      setTimeout(() => {
        throw new Error('wow');
      }, 10);
    }
  };
}

Any ideas?

bug enhancement help wanted

Most helpful comment

With the current implementation, middlewares do not support promises as return object (which is essentially what happens behind the scenes of async functions).

I think there's an opportunity here to add support for this to allow middy to be even more expressive and simple to use.

I will investigate this option and add few test cases similar to the ones you mentioned here. Feel free to contribute to some proposal if you want.

At a first glance, my current expectation is that we should handle the possibility that a middleware returns a function here. If this happens, we should use attach to the returned promise a .then(runNext) and handler the possible rejection with .catch((err) => done(err)) (or .catch(done) in brief).

But being late at night, that's just me speculating 馃槢

Thanks for the meaningful reporting though!

@dkatavic what do you think here?

All 3 comments

Passing the error to next seems to work, but it might be cool if these were caught without that. 馃檪

With the current implementation, middlewares do not support promises as return object (which is essentially what happens behind the scenes of async functions).

I think there's an opportunity here to add support for this to allow middy to be even more expressive and simple to use.

I will investigate this option and add few test cases similar to the ones you mentioned here. Feel free to contribute to some proposal if you want.

At a first glance, my current expectation is that we should handle the possibility that a middleware returns a function here. If this happens, we should use attach to the returned promise a .then(runNext) and handler the possible rejection with .catch((err) => done(err)) (or .catch(done) in brief).

But being late at night, that's just me speculating 馃槢

Thanks for the meaningful reporting though!

@dkatavic what do you think here?

@lmammino I like what you have proposed in PR #51 , async middlewares should be supported out of the box 馃憤

Was this page helpful?
0 / 5 - 0 ratings