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

Order of attaching middleware #707

Closed
akshay-nm opened this issue Aug 13, 2021 · 2 comments
Closed

Order of attaching middleware #707

akshay-nm opened this issue Aug 13, 2021 · 2 comments
Milestone

Comments

@akshay-nm
Copy link

The docs suggest that we should add the error handler after all middleware, but this approach fails as if the validation middleware throws an error, the next middleware is not even attached.

What the docs suggest?

// Let's "middyfy" our handler, then we will be able to attach middlewares to it
const handler = middy(baseHandler)
  .use(jsonBodyParser()) // parses the request body when it's a JSON and converts it to an object
  .use(validator({inputSchema})) // validates the input
  .use(httpErrorHandler()) // handles common http errors and returns proper responses

module.exports = { handler }

What it should suggest?

// Let's "middyfy" our handler, then we will be able to attach middlewares to it
const handler = middy(baseHandler)
  .use(httpErrorHandler()) // handles common http errors and returns proper responses
  .use(jsonBodyParser()) // parses the request body when it's a JSON and converts it to an object
  .use(validator({inputSchema})) // validates the input

module.exports = { handler }

The httpErrorHandler should be the first middleware to be attached.

@willfarrell willfarrell added this to the v3.0.0 milestone Oct 1, 2021
@willfarrell
Copy link
Member

Thanks for reporting. This is a very simple example and doesn't have complete error handling (See #720). Typically httpErrorHandler at the end to catch any errors thrown by any after middlewares. Improving how error handing works is planned for the next version of middy. I've tagged this for review when we tackle this.

@willfarrell willfarrell mentioned this issue Dec 27, 2021
26 tasks
@willfarrell
Copy link
Member

This is now included in the v3 branch and will be release later this month.

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

No branches or pull requests

2 participants